/*
 * 2014.12.03
 * wzwz
 * Change Log:
 * Date         Author      Note
 * 2015.04.03   wzwz        转换双机头机头反馈
 * 2015.04.17   wzwz        增加选针器单个制定动作
 * 2015.04.21   wzwz        转换双系统的三角状态
 * 2015.04.22   wzwz        拷贝速度，拉布等
 * 2015.04.23   wzwz        恒强永久行定
 * 2015.04.24   wzwz        度目不动作时的bug
 * 2015.05.18   wzwz        修改起底板功能
 * 2015.05.19   wzwz        增加三角和纱嘴类型设置
 * 2015.05.19   wzwz        密度马达分2次动作
 * 2015.05.19   wzwz        伺服类型
 * 2015.05.19   wzwz        编织时间不计算停车时间
 * 2015.06.29   wzwz        辛克方向设置bug
 * 2015.07.03   wzwz        复位前清空参数修改标志
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mh/common.h>
#include <mh/sram.h>
#include <mh/mac.h>
#include "knit.h"
#include <mh/mh_file.h>
#include <mh/probe.h>
#include <sys/stat.h>
#include "prefixknit.h"
#include "backpiece.h"
#include "pthread.h"
#include "carriage.h"
#include "ver.h"
#include "board.h"



/*
 * create knit memory data
 * param cam: cams of system, from 1 to 4
 */
MH_KNIT* CreateKnitData(uchar cam)
{
	MH_KNIT *p = (MH_KNIT*)calloc(1, sizeof(MH_KNIT));
	if(p)
	{
	    struct stat buf;
	    if(stat(SRAM_DEV_FILE, &buf))
	    {
	        p->communication = COMM_INET;
	        p->pmac = CreateMachine(cam);
	    }
	    else
	    {
	        p->communication = COMM_SRAM;
	        p->pmac = CreateSram(cam);
	    }
	    p->sys_info.cam = cam;
	    p->mac_speed_switch = STATE_STOP;
	    InitPayment(p);
	    if(!p->pmac)
	    {
	        free(p);
	        p = NULL;
	    }
	}
	return p;
}
/*
 *
 */
uchar GetCompany(MH_KNIT *p)
{
    return p->company;
}
/*
 * set company of machine
 */
void SetCompany(MH_KNIT *p, uchar company)
{
    p->company = company;
}
/*
 * get need to which
 */
uchar GetWhichNeedDo(MH_KNIT *p)
{
    uchar ret = NEED_DO_NONE;
    if(p->need_do_which == NEED_DO_RESET_START)
    {
        ret = NEED_DO_RESET_START;
    }
    else if(!p->knit_state.reset_finished)
    {
        ret = NEED_DO_WAIT_RESET;
    }
    else if(p->knit_state.process == KNIT_PROCESS_NORMAL_RUN)
    {
        ret = NEED_DO_GOON_KNIT;
    }
    return ret;
}
/*
 * set need do which
 */
void SetWhichNeedDo(MH_KNIT *p, uchar need_do)
{
    if(need_do == NEED_DO_RESET_START)
    {
        p->param_change = FALSE;
        p->need_do_which = need_do;
    }
    else if(need_do == NEED_DO_WAIT_RESET)
    {
        p->knit_state.reset_finished = FALSE;
    }
    else if(need_do == NEED_DO_GOON_KNIT)
    {
        p->knit_state.reset_finished = TRUE;
        p->knit_state.process = KNIT_PROCESS_NORMAL_RUN;
    }
    else
    {
        p->need_do_which = need_do;
    }
}
/*
 * safe delete memory data
 */
void DestroyKnitData(MH_KNIT *p)
{
    if(p->th_run)
    {
        p->th_request = TH_STATE_EXIT;
        pthread_join(p->th_run, NULL);
    }
	if(p->flower_data)
	{
		free(p->flower_data);
	}
	if(p->ctrl_page)
	{
	    free(p->ctrl_page);
	}
	if(p->knit_page_head)
	{
	    free(p->knit_page_head);
	}
	pk_clear_data(p);
	backpiece_free(p);
	SafeFreeAuxConList(p);
	while(p->log_list.next)
	{
	    SYS_LOG *next = p->log_list.next;
	    p->log_list.next = next->next;
	    free(next);
	}
	while(p->peco_list)
	{
	    ECONOMY_LIST *q = p->peco_list;
	    p->peco_list = q->next;
	    free(q);
	}
	if(p->pmac)
	{
	    if(p->communication == COMM_SRAM)
	    {
	        DestroySram(p->pmac);
	    }
	    else
	    {
	        DestroyMachine(p->pmac);
	    }
	    free(p->pmac);
	}
	free(p);
}
/*
 * create every page for knitting
 */
KNIT_PAGE* CreateKnitPage(MH_KNIT *p, ushort n)
{
    if(p->knit_page_head)
    {
        free(p->knit_page_head);
        p->knit_page_head = NULL;
        p->knit_page = NULL;
    }
    p->knit_page_head = (KNIT_PAGE*)calloc(n + 1, sizeof(KNIT_PAGE));
    memset(p->knit_page_head, 0, (n + 1) * sizeof(KNIT_PAGE));
    if(p->knit_page_head)
    {
        p->knit_page = &p->knit_page_head[1];
        p->total_line = n;
    }
    else
    {
        p->total_line = 0;
    }
    return p->knit_page;
}
/*
 * free old data and create flower data
 */
uchar* CreateFlowerData(MH_KNIT *p, ushort w, ushort h)
{
    if(p->flower_data)
    {
        free(p->flower_data);
    }
    p->flower_data = (uchar*)calloc(h, w);
    if(p->flower_data)
    {
        p->flower_width = w;
        p->flower_height = h;
    }
    else
    {
        p->flower_width = 0;
        p->flower_height = 0;
    }
    return p->flower_data;
}
/*
 * x, y start from 0
 */
uchar GetFlowerCode(MH_KNIT *p, ushort x, ushort y)
{
    uchar ret = 0;
    if(x < p->flower_width && y < p->flower_height && p->flower_data)
    {
        ret = p->flower_data[p->flower_width * y + x];
    }
    return ret;
}
/*
 * x, y start from 0
 */
void SetFlowerCode(MH_KNIT *p, ushort x, ushort y, uchar code)
{
    if(x < p->flower_width && y < p->flower_height && p->flower_data)
    {
        p->flower_data[p->flower_width * y + x] = code;
    }
}
/*
 * set flower data to knit data
 */
void SetFlowerData(MH_KNIT *p, uchar *data)
{
    p->flower_data = data;
}
/*
 * get flower data from knit data
 */
uchar* GetFlowerData(MH_KNIT *p)
{
    return p->flower_data;
}
/*
 * get flower line from knit data
 * param n: line start from 1
 */
uchar* GetFlowerLine(MH_KNIT *p, ushort n)
{
    uchar *q = 0;
    if(p->flower_data)
    {
        if (n > 0 && n <= p->flower_height)
        {
            q = &p->flower_data[(n - 1) * p->flower_width];
        }
    }
    return q;
}
/*
 * get width of flower
 */
ushort GetFlowerWidth(MH_KNIT *p)
{
    return p->flower_width;
}
/*
 * set width of flower
 */
void SetFlowerWidth(MH_KNIT *p, ushort n)
{
    p->flower_width = n;
}
/*
 * get height of flower
 */
ushort GetFlowerHeight(MH_KNIT *p)
{
    return p->flower_height;
}
/*
 * set height of flower
 */
void SetFlowerHeight(MH_KNIT *p, ushort n)
{
    p->flower_height = n;
}
/*
 * set control data to knit data
 */
void SetControlData(MH_KNIT *p, KNIT_PAGE *data)
{
    p->knit_page = data;
}
/*
 * get control data from data
 */
KNIT_PAGE* GetControlData(MH_KNIT *p)
{
    return p->knit_page;
}
/*
 * get control page from data
 * param n: knit line, 1-start
 */
KNIT_PAGE* GetControlPage(MH_KNIT *p, ushort n)
{
    KNIT_PAGE *q = 0;
    if(p->knit_page)
    {
        if(n > 0 && n <= p->total_line)
        {
            q = &p->knit_page[n - 1];
        }
    }
    return q;
}
/*
 * line: start from 1
 */
const KNIT_PAGE* GetRunPage(MH_KNIT *p, unsigned short line)
{
    //printf("line: %d, %d\n", p->knit_state.prefix, line);
    KNIT_PAGE *q = 0;
    if(p->knit_state.prefix)
    {
        if(line > 0 && line <= p->pk_data.pcnt.rows)
        {
            q = (KNIT_PAGE*)&p->pk_data.pcnt.buf[(line - 1) * sizeof(KNIT_PAGE)];
        }
    }
    else if(p->aux_list.index > -1 && p->aux_list.index < p->aux_list.num)
    {
        q = &p->aux_list.paux_con[p->aux_list.index].pcon[line - 1];
    }
    else
    {
        q = GetControlPage(p, line);
    }
    return q;
}
/*
 *
 */
int GetPage4CamLock(MH_KNIT *p, KNIT_PAGE *pcon, ushort line)
{
    int ret = 0;
    //cam lock
    if(p->ctrl_page && line > 0 && line <= p->total_line && p->cam_lock && p->sys_info.cam > 1)
    {
        if(p->ctrl_page[line - 1].threhold <= 0)
        {
            ret = SwitchLockCam(pcon, p->cam_lock, line & 1);
        }
    }
    return ret;
}
/*
 * get current knit line
 * return: current knit line,1-start
 */
ushort GetCurrentKnitLine(MH_KNIT *p)
{
    return p->curt_line + 1;
}
/*
 * set current knit line
 * param n: current knit line, 1-start
 */
void SetCurrentKnitLine(MH_KNIT *p, ushort n)
{
    p->curt_line = n - 1;
}
/*
 * get total knit page
 */
ushort GetTotalKnitPage(MH_KNIT *p)
{
    return p->total_line;
}
/*
 * set total knit page
 */
void SetTotalKnitPage(MH_KNIT *p, ushort n)
{
    p->total_line = n;
}
/*
 * get knit file name
 */
char* GetKnitFileName(MH_KNIT *p)
{
    return p->file_name;
}
/*
 * set knit file name
 */
void SetKnitFileName(MH_KNIT *p, char *file)
{
    p->file_name[0] = 0;
    int nLen = strlen(file);
    if(nLen > 0 && nLen < sizeof(p->file_name))
    {
        strcat(p->file_name, file);
        p->knit_state.prefix = FALSE;
    }
}
/*
 * get knit file type
 */
uchar GetKnitFileType(MH_KNIT *p)
{
    return p->file_type;
}
/*
 * set knit file type
 */
void SetKnitFileType(MH_KNIT *p, uchar n)
{
    p->file_type = n;
}
/*
 * get machine state
 */
int GetMacGetSysState(MH_KNIT *p, ushort *state, ushort *reset_state)
{
    int ret = MacGetSysState(p->pmac, state, reset_state);
    return ret;
}

/*
 * get current needle (position of machine)
 */
int GetCurrentNeedle(MH_KNIT *p, short *needle)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            int pulse = 0;
            ret = SramGetCurrentPulse(p->pmac, &pulse);
            if(p->belt_right_com - p->belt_left_com > 0)
            {
                *needle = (pulse - p->belt_left_com) * p->total_needle / (p->belt_right_com - p->belt_left_com);
            }
        }
        else
        {
            ret = MacGetCurrentNeedle(p->pmac, needle);
        }
    }
    return ret;
}
/*
 * set current needle
 */
void SetCurrentNeedle(MH_KNIT *p, int n)
{
    //p->curt_needle = n;
}
/*
 * get current pulse (pulse of driver)
 */
int GetCurrentPulse(MH_KNIT *p, int *pulse)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramGetCurrentPulse(p->pmac, pulse);
        }
        else
        {
            ret = MacGetCurrentPulse(p->pmac, pulse);
        }
    }
    return ret;
}
/*
 * get total cloth
 */
uint GetTotalCloth(MH_KNIT *p)
{
    return p->total_cloth;
}
/*
 * set total cloth
 */
int SetTotalCloth(MH_KNIT *p, uint n)
{
    int ret = 0;
    if(n > p->done_cloth)
    {
        p->total_cloth = n;
    }
    return ret;
}
/*
 * get total cloth
 */
uint GetTotalClothBackup(MH_KNIT *p)
{
    return p->total_cloth_backup;
}
/*
 * set total cloth
 */
int SetTotalClothBackup(MH_KNIT *p, uint n)
{
    int ret = 0;
    p->total_cloth_backup = n;
    return ret;
}
/*
 * get finished cloth
 */
uint GetDoneCloth(MH_KNIT *p)
{
    return p->done_cloth;
}
/*
 * set finished cloth
 */
void SetDoneCloth(MH_KNIT *p, uint n)
{
    p->done_cloth = n;
}
/*
 * get finished cloth
 */
uint GetDoneClothBackup(MH_KNIT *p)
{
    return p->done_cloth_backup;
}
/*
 * set finished cloth
 */
void SetDoneClothBackup(MH_KNIT *p, uint n)
{
    p->done_cloth_backup = n;
}
/*
 * add finished cloth
 */
void AddDoneCloth(MH_KNIT *p, uint n)
{
    p->done_cloth += n;
    if(!p->knit_state.bp_enable && !p->knit_state.fp_enable)
    {
        p->done_cloth_backup += n;
    }
}
/*
 * get time for knitting one piece of cloth
 */
uint GetKnitTime(MH_KNIT *p)
{
    return p->knit_time;
}
/*
 * set time for knitting one piece of cloth
 */
void SetKnitTime(MH_KNIT *p, uint n)
{
    p->knit_time = n;
}
/*
 * get time for work
 */
uint GetWorkTime(MH_KNIT *p)
{
    return p->run_time;
}
/*
 * add time for work
 */
uint AddWorkTime(MH_KNIT *p, uint n)
{
    p->run_time += n;
    if(p->mac_speed_switch != STATE_STOP)
    {
        p->knit_count_time += n;
    }
    return p->run_time;
}
/*
 *
 */
 uchar GetMacSpeedSwitch(MH_KNIT *p)
 {
     return p->mac_speed_switch;
 }
 /*
 *
 */
uint GetKnitCountTime(MH_KNIT *p)
{
    return p->knit_count_time;
}
/*
 *
 */
void SetKnitCountTime(MH_KNIT *p, uint n)
{
    p->knit_count_time = n;
}
/*
 * set time for work
 */
void SetWorkTime(MH_KNIT *p, uint n)
{
    p->run_time = n;
}
/*
 * get time of system
 */
time_t GetSysTime(MH_KNIT *p)
{
    return p->sys_time;
}
/*
 * set time of system
 */
void SetSysTime(MH_KNIT *p, time_t t)
{
    p->sys_time = t;
}
/*
 * get position for needle zero signal
 */
int GetNeedleZero(MH_KNIT *p)
{
    return p->needle_zero;
}
/*
 * set position for needle zero signal
 */
void SetNeedleZero(MH_KNIT *p, int n)
{
    p->needle_zero = n;
}
/*
 * get yarn's compensation to right on left cam
 */
int GetLeftYarnRightCom(MH_KNIT *p)
{
    return p->left_yarn_right_com;
}
/*
 * set yarn's compensation to right on left cam
 */
void SetLeftYarnRightCom(MH_KNIT *p, int n)
{
    p->left_yarn_right_com = n;
}
/*
 * get yarn's compensation to left on left cam
 */
int GetLeftYarnLeftCom(MH_KNIT *p)
{
    return p->left_yarn_left_com;
}
/*
 * set yarn's compensation to left on left cam
 */
void SetLeftYarnLeftCom(MH_KNIT *p, int n)
{
    p->left_yarn_left_com = n;
}
/*
 * get yarn's compensation to right on right cam
 */
int GetRightYarnRightCom(MH_KNIT *p)
{
    return p->right_yarn_right_com;
}
/*
 * set yarn's compensation to right on right cam
 */
void SetRightYarnRightCom(MH_KNIT *p, int n)
{
    p->right_yarn_right_com = n;
}
/*
 * get yarn's compensation to left on right cam
 */
int GetRightYarnLeftCom(MH_KNIT *p)
{
    return p->right_yarn_left_com;
}
/*
 * set yarn's compensation to left on right cam
 */
void SetRightYarnLeftCom(MH_KNIT *p, int n)
{
    p->right_yarn_left_com = n;
}
/*
 *get forbidden position on left
 */
int GetLeftForbidPos(MH_KNIT *p)
{
    return p->left_forbid;
}
/*
 * set forbidden position on left
 */
void SetLeftForbidPos(MH_KNIT *p, int n)
{
    p->left_forbid = n;
}
/*
 * get forbidden position on right
 */
int GetRightForbidPos(MH_KNIT *p)
{
    return p->right_forbid;
}
/*
 * set forbidden position on right
 */
void SetRightForbidPos(MH_KNIT *p, int n)
{
    p->right_forbid = n;
}
/*
 * get direction of machine
 */
uchar GetMacDrt(MH_KNIT *p)
{
    return p->mac_drt;
}
/*
 * set direction of machine
 */
void SetMacDrt(MH_KNIT *p, uchar n)
{
    p->mac_drt = n;
}
/*
 *get needle per inch
 */
float GetInchType(MH_KNIT *p)
{
    return p->inch_type;
}
/*
 * set needle per inch
 */
void SetInchType(MH_KNIT *p, float n)
{
    p->inch_type = n;
}
/*
 * get selector's section
 */
uchar GetSelectSect(MH_KNIT *p)
{
    return p->select_sect;
}
/*
 * set selector's section
 */
void SetSelectSect(MH_KNIT *p, uchar n)
{
    p->select_sect = n;
}
/*
 * get piece of select
 */
uchar GetSelectPiece(MH_KNIT *p)
{
    uchar ret = 0;
    int i = 0;
    for(i = 0; i < p->select_sect; ++i)
    {
        ret |= 1 << p->select[i];
    }
    return ret;
}
/*
 * set reserved selector.s section when select_sect up is zero
 */
void SetSelectPiece(MH_KNIT *p, uchar piece)
{
    int i = 0, j = 0;
    uchar select[8] = {0};
    for(i = 0; i < 8; ++i)
    {
        if(piece & (1 << i))
        {
            select[j] = i;
            ++j;
        }
    }
    if(p->mac.select_array)
    {
        for(i = 0; i < j; ++i)
        {
            p->select[i] = select[j - 1 - i];
        }
    }
    else
    {
        for(i = 0; i < j; ++i)
        {
            p->select[i] = select[i];
        }
    }
}
/*
 *
 */
uchar GetSelectPieceData(MH_KNIT *p, uchar n)
{
    return p->select[n];
 }
 /*
 *
 */
void  SetSelectPieceData(MH_KNIT *p, uchar n, uchar array)
{
     p->select[n] = array;
 }
  /*
 *
 */
uint GetSelectArray(MH_KNIT *p)
{
    return p->mac.select_array;
}
/*
 *
 */
void SetSelectArray(MH_KNIT *p, uint array)
{
    p->mac.select_array = array;
}
/*
 *
 */
uchar GetSelectStartSect(MH_KNIT *p)
{
    return p->select_sect_start;
}
/*
 *
 */
void SetSelectStartSect(MH_KNIT *p, uchar sect)
{
    p->select_sect_start = sect;
}
/*
 * get high voltage for cam
 */
uchar GetCamHighVoltage(MH_KNIT *p)
{
    return p->cam_high_voltage;
}
/*
 * set high voltage for cam
 */
void SetCamHighVoltage(MH_KNIT *p, uchar n)
{
    p->cam_high_voltage = n;
}
/*
 * get low voltage for cam
 */
uchar GetCamLowVoltage(MH_KNIT *p)
{
    return p->cam_low_voltage;
}
/*
 * set low voltage for cam
 */
void SetCamLowVoltage(MH_KNIT *p, uchar n)
{
    p->cam_low_voltage = n;
}
/*
 * get high voltage for selector
 */
uchar GetSelectHighVoltage(MH_KNIT *p)
{
    return p->select_high_voltage;
}
/*
 * set high voltage for selector
 */
void SetSelectHighVoltage(MH_KNIT *p, uchar n)
{
    p->select_high_voltage = n;
}
/*
 * get low voltage for selector
 */
uchar GetSelectLowVoltage(MH_KNIT *p)
{
    return p->select_low_voltage;
}
/*
 * set low voltage for selector
 */
void SetSelectLowVoltage(MH_KNIT *p, uchar n)
{
    p->select_low_voltage = n;
}
/*
 * get high voltage for gauze
 */
uchar GetGauzeHighVoltage(MH_KNIT *p)
{
    return p->gauze_high_voltage;
}
/*
 * set high voltage for gauze
 */
int SetGauzeHighVoltage(MH_KNIT *p, uchar value)
{
    int ret = 0;
    p->gauze_high_voltage = value;
    if(p->machine_ok)
    {
        //
    }
    return ret;
}
/*
 * get select index
 */
void GetSelect(MH_KNIT *p, uchar *select)
{
    memcpy(select, p->select, sizeof(p->select));
}
/*
 * set select index
 */
int SetSelect(MH_KNIT *p, uchar *select)
{
    int ret = 0;
    memcpy(p->select, select, sizeof(p->select));
    if(p->machine_ok)
    {
        //
    }
    return ret;
}
/*
 * get network state
 */
uchar GetNetworkState(MH_KNIT *p)
{
    return p->net_state;
}
/*
 * set network state
 */
void SetNetworkState(MH_KNIT *p, uchar n)
{
    p->net_state = n;
}
/*
 *
 */
 void SetRunState(MH_KNIT *p, uchar n)
 {
     p->run_state = n;
 }
 /*
 *
 */
 uchar GetRunStata(MH_KNIT *p)
 {
     return p->run_state;
 }
 /*
 *
 */
 void SetRunResetStata(MH_KNIT *p, uchar n)
 {
     p->run_reset_state = n;
 }
 /*
 *
 */
 uchar GetRunResetState(MH_KNIT *p)
 {
     return p->run_reset_state;
 }
  /*
 *
 */
 void SetBarState(MH_KNIT *p, uchar n)
 {
     p->bar_state = n;
 }
 /*
 *
 */
 uchar GetBarState(MH_KNIT *p)
 {
     return p->bar_state;
 }
/*
 * get sys info
 */
void GetSysInfo(MH_KNIT *p, void *info)
{
    memcpy(info, &p->sys_info, sizeof(p->sys_info));
}
/*
 * set sys info
 */
int SetSysInfo(MH_KNIT *p, void *info)
{
    int ret = 0;
    uchar cam = p->sys_info.cam;
    memcpy(&p->sys_info, info, sizeof(p->sys_info));
    if(cam)
    {
        p->sys_info.cam = cam;
    }
    if(p->machine_ok)
    {
        //
    }
    return ret;
}
/*
 * get time for screen saver
 */
uchar GetSaveScreenTime(MH_KNIT *p)
{
    return p->save_screen_time;
}
/*
 *
 */
void SetSaveScreenTime(MH_KNIT *p, uint time_min)
{
    p->save_screen_time = time_min;
}

/*
 * get enable for screen saver
 */
uchar GetSaveScreenEnable(MH_KNIT *p)
{
    return p->save_screen_enable;
}
/*
 * set enable for screen saver
 */
void SetSaveScreenEnable(MH_KNIT *p, uchar n)
{
    p->save_screen_enable = n;
}
/*
 * get selector's compensation to right
 */
int GetSelectRightCom(MH_KNIT *p)
{
    return p->select_right_com;
}
/*
 * set selector's compensation to right
 */
void SetSelectRightCom(MH_KNIT *p, int n)
{
    p->select_right_com = n;
}
/*
 * get selector's compensation to left
 */
int GetSelectLeftCom(MH_KNIT *p)
{
    return p->select_left_com;
}
/*
 * set selector's compensation to left
 */
void SetSelectLeftCom(MH_KNIT *p, int n)
{
    p->select_left_com = n;
}
/*
 * get type of cam's control
 */
uchar GetCamType(MH_KNIT *p)
{
    return p->cam_type;
}
/*
 *
 */
void SetCamType(MH_KNIT *p, uchar type)
{
    p->cam_type = type;
}
/*
 * get speed for dumu's reset
 */
uchar GetDumuResetSpeed(MH_KNIT *p)
{
    return p->dumu_reset_speed;
}
/*
 * set speed for dumu's reset
 */
void SetDumuResetSpeed(MH_KNIT *p, uchar n)
{
    p->dumu_reset_speed = n;
}
/*
 * get speed for dumu's work
 */
uchar GetDumuWorkSpeed(MH_KNIT *p)
{
    return p->dumu_work_speed;
}
/*
 * set speed for dumu's work
 */
void SetDumuWorkSpeed(MH_KNIT *p, uchar n)
{
    p->dumu_work_speed = n;
}
/*
 * get max delta for dumu work
 */
uchar GetDumuMaxDelta(MH_KNIT *p)
{
    return p->dumu_work_delta;
}
/*
 * set max delta for dumu work
 */
void SetDumuMaxDelta(MH_KNIT *p, uchar n)
{
    p->dumu_work_delta = n;
}
/*
 * get delta for dumu reset
 */
uchar GetDumuResetDelta(MH_KNIT *p)
{
    return p->dumu_reset_delta;
}
/*
 * set delta for dumu reset
 */
void SetDumuResetDelta(MH_KNIT *p, uchar n)
{
    p->dumu_reset_delta = n;
}
/*
 * get com for dumu recv
 */
uchar GetDumuRecvCom(MH_KNIT *p)
{
    return p->dumu_recv_com;
}
/*
 * set com for dumu recv
 */
void SetDumuRecvCom(MH_KNIT *p, uchar n)
{
    p->dumu_recv_com = n;
}
/*
 * get enable of dumu before
 */
uchar GetDumuBeforeEnable(MH_KNIT *p)
{
    return p->dumu_before_enable;
}
/*
 * set enable of dumu before
 */
void SetDumuBeforeEnable(MH_KNIT *p, uchar n)
{
    p->dumu_before_enable = n;
}
/*
 * get enable of dumu reset warn
 */
uchar GetDumuResetWarnEnable(MH_KNIT *p)
{
    return p->dumu_reset_warn_enable;
}
/*
 * set enable of dumu reset warn
 */
void SetDumuResetWarnEnable(MH_KNIT *p, uchar n)
{
    p->dumu_reset_warn_enable = n;
}
/*
 * get speed of sink work
 */
uchar GetSinkWorkSpeed(MH_KNIT *p)
{
    return p->sink_work_speed;
}
/*
 * set speed of sink work
 */
void SetSinkWorkSpeed(MH_KNIT *p, uchar n)
{
    p->sink_work_speed = n;
}
/*
 * get wait for yarn feeder start
 */
ushort GetFeederStartWait(MH_KNIT *p)
{
    return p->feeder_start_wait;
}
/*
 * set wait for yarn feeder start
 */
void SetFeederStartWait(MH_KNIT *p, ushort n)
{
    p->feeder_start_wait = n;
}
/*
 * get wait for yarn feeder stop
 */
ushort GetFeederStopWait(MH_KNIT *p)
{
    return p->feeder_stop_wait;
}
/*
 * set wait for yarn feeder stop
 */
void SetFeederStopWait(MH_KNIT *p, ushort n)
{
    p->feeder_stop_wait = n;
}
/*
 * get enable of special run
 */
uchar GetSpecialRunEnable(MH_KNIT *p)
{
    return p->run_special_enable;
}
/*
 * set enable of special run
 */
void SetSpecialRunEnable(MH_KNIT *p, uchar n)
{
    p->run_special_enable = n;
}
/*
 * get type of rab run
 */
uchar GetRabRunType(MH_KNIT *p)
{
    return p->rab_run_type;
}
/*
 * set type of rab run
 */
void SetRabRunType(MH_KNIT *p, uchar n)
{
    p->rab_run_type = n;
}
/*
 * get stop torque of roll
 */
uchar GetRollStopTorque(MH_KNIT *p)
{
    return p->roll_stop_torque;
}
/*
 * set stop torque of roll
 */
void SetRollStopTorque(MH_KNIT *p, uchar n)
{
    p->roll_stop_torque = n;
}
/*
 * get enable of second dumu
 */
uchar GetSecondDumuEnable(MH_KNIT *p)
{
    return p->second_dumu_enable;
}
/*
 * set enable of second dumu
 */
void SetSecondDumuEnable(MH_KNIT *p, uchar n)
{
    p->second_dumu_enable = n;
}
/*
 * get com for second dumu(needle)
 */
char GetSecondDumuCom(MH_KNIT *p)
{
    return p->second_dumu_com;
}
/*
 * set com for second dumu
 */
void SetSecondDumuCom(MH_KNIT *p, char n)
{
    p->second_dumu_com = n;
}
/*
 *
 */
uchar GetYarnFeederMode(MH_KNIT *p)
{
    return p->yarn_feeder_mode;
}
/*
 *
 */
void SetYarnFeederMode(MH_KNIT *p, uchar mode)
{
    p->yarn_feeder_mode = mode;
}
/*
 * get line for slow because knot
 */
uchar GetKnotWaitLine(MH_KNIT *p)
{
    return p->knot_wait_line;
}
/*
 * set line for slow because knot
 */
void SetKnotWaitLine(MH_KNIT *p, uchar n)
{
    p->knot_wait_line = n;
}
/*
 * get total needle
 */
ushort GetTotalNeedle(MH_KNIT *p)
{
    return p->total_needle;
}
/*
 * set total needle
 */
void SetTotalNeedle(MH_KNIT *p, ushort n)
{
    p->total_needle = n;
}
/*
 * get compensation for machine to left
 */
int GetMacLeftCom(MH_KNIT* p)
{
    return p->belt_left_com;
}
/*
 * set compensation for machine to left
 */
void SetMacLeftCom(MH_KNIT *p, int n)
{
    p->belt_left_com = n;
}
/*
 * get compemsation for machine to right
 */
int GetMacRightCom(MH_KNIT *p)
{
    return p->belt_right_com;
}
/*
 * set compensation for machine to right
 */
void SetMacRightCom(MH_KNIT *p, int n)
{
    p->belt_right_com = n;
}
/*
 * get compensation for belt pitch
 */
int GetBeltPitchAdj(MH_KNIT *p)
{
    //return p->belt_pitch_adjust;
    return p->belt_right_com - p->belt_left_com;
}
/*
 * set compensation for belt pitch
 */
void SetBeltPitchAdj(MH_KNIT *p, int n)
{
    p->belt_pitch_adjust = n;
}
/*
 * get current pulse of machine
 */
int GetMacCurtPulse(MH_KNIT *p)
{
    return p->belt_curt_pulse;
}
/*
 * get rock speed
 */
uchar GetRockSpeed(MH_KNIT *p)
{
    return p->rock_speed;
}
/*
 * set rock speed
 */
void SetRockSpeed(MH_KNIT *p, uchar n)
{
    p->rock_speed = n;
}
/*
 * get rock mid speed
 */
uchar GetRockMidSpeed(MH_KNIT *p)
{
    return p->rock_mid_speed;
}
/*
 * set rock mid speed
 */
void SetRockMidSpeed(MH_KNIT *p, uchar n)
{
    p->rock_mid_speed = n;
}
/*
 * get rock low speed
 */
uchar GetRockLowSpeed(MH_KNIT *p)
{
    return p->rock_low_speed;
}
/*
 * set rock low speed
 */
void SetRockLowSpeed(MH_KNIT *p, uchar n)
{
    p->rock_low_speed = n;
}
/*
 * get lock of cam
 */
uchar GetCamLock(MH_KNIT *p)
{
    return p->cam_lock;
}
/*
 * set lock of cam
 */
void SetCamLock(MH_KNIT *p, uchar lock)
{
    p->cam_lock = lock;
}
/*
 * get sink enable
 */
uchar GetSinkEnable(MH_KNIT *p)
{
    return p->sink_enable;
}
/*
 * set sink enable
 */
void SetSinkEnable(MH_KNIT *p, uchar n)
{
    p->sink_enable = n;
}
/*
 * get language
 */
uchar GetLanguage(MH_KNIT *p)
{
    return p->language;
}
/*
 * set language
 */
void SetLanguage(MH_KNIT *p, uchar n)
{
    p->language = n;
}
/*
 * get selector repeat
 */
uchar GetSelectRepeatEnable(MH_KNIT *p)
{
    return p->select_repeat_enable;
}
/*
 * set selector repeat
 */
void SetSelectRepeatEnable(MH_KNIT *p, uchar n)
{
    p->select_repeat_enable = n;
}
/*
 * get machine run out enable
 */
uchar GetMacOutEnable(MH_KNIT *p)
{
    return p->mac_out_enable;
}
/*
 * set machine run out enable
 */
void SetMacOutEnable(MH_KNIT *p, uchar n)
{
    p->mac_out_enable = n;
}
/*
 * 高罗拉
 */
int GetRabEnable(MH_KNIT *p)
{
    return p->knit_state.rab_enable;
}
/*
 * high roller
 */
void SetRabEnable(MH_KNIT *p, int enable)
{
    p->knit_state.rab_enable = enable;
}
/*
 * high roller
 */
ushort GetRabMinParam(MH_KNIT *p)
{
    return p->rab_min_param;
}
/*
 * high roller
 */
void SetRabMinParam(MH_KNIT *p, ushort n)
{
    p->rab_min_param = n;
}
/*
 * high roller
 */
ushort GetRabMaxParam(MH_KNIT *p)
{
    return p->rab_max_param;
}
/*
 *
 */
void SetRabMaxParam(MH_KNIT *p, ushort n)
{
    p->rab_max_param = n;
}
/*
 * 低罗拉(主拉布)
 */
int GetRollEnable(MH_KNIT *p)
{
    return p->knit_state.roll_enable;
}
/*
 *
 */
void SetRollEnable(MH_KNIT *p, int enable)
{
    p->knit_state.roll_enable = enable;
}
/*
 * get rock type
 */
uchar GetRockType(MH_KNIT *p)
{
    return p->rock_type;
}
/*
 * set rock type
 */
void SetRockType(MH_KNIT *p, uchar n)
{
    p->rock_type = n;
}
/*
 * get rab percent
 */
uchar GetRabPercent(MH_KNIT *p)
{
    return p->rab_percent;
}
/*
 * set rab percent
 */
void SetRabPercent(MH_KNIT *p, uchar n)
{
    p->rab_percent = n;
}
/*
 * get low speed of machine
 */
uchar GetMacLowSpeed(MH_KNIT *p)
{
    return p->mac_low_speed;
}
/*
 * set low speed of machine
 */
void SetMacLowSpeed(MH_KNIT *p, uchar n)
{
    p->mac_low_speed = n;
}
/*
 * get mid speed of machine
 */
uchar GetMacMidSpeed(MH_KNIT *p)
{
    return p->mac_mid_speed;
}
/*
 * set mid speed of machine
 */
void SetMacMidSpeed(MH_KNIT *p, uchar n)
{
    p->mac_mid_speed = n;
}
/*
 * get reset speed of machine
 */
uchar GetMacResetSpeed(MH_KNIT *p)
{
    return p->mac_reset_speed;
}
/*
 * set reset speed of machine
 */
void SetMacResetSpeed(MH_KNIT *p, uchar n)
{
    p->mac_reset_speed = n;
}
/*
 *
 */
uchar GetMacEmptySpeed(MH_KNIT *p)
{
    return p->mac_empty_speed;
}
/*
 *
 */
void SetMacEmptySpeed(MH_KNIT *p, uchar speed)
{
    p->mac_empty_speed = speed;
}
/*
 * get high speed of machine
 */
uchar GetMacHighSpeed(MH_KNIT *p)
{
    return p->mac_high_speed;
}
/*
 * set high speed of machine
 */
void SetMacHighSpeed(MH_KNIT *p, uchar n)
{
    p->mac_high_speed = n;
}
/*
 * get high line speed of machine
 */
uchar GetMacHighLineSpeed(MH_KNIT *p)
{
    return p->mac_high_line_speed;
}
/*
 * set high line speed of machine
 */
void SetMacHighLineSpeed(MH_KNIT *p, uchar n)
{
    p->mac_high_line_speed = n;
}

/*
 * get start needle for knitting
 */
ushort GetStartNeedle(MH_KNIT *p)
{
    return p->start_needle;
}
/*
 * set start needle for knitting
 */
void SetStartNeedle(MH_KNIT *p, ushort n)
{
    p->start_needle = n;
}
/*
 * get compensation for rock gap
 */
int GetRockGapCom(MH_KNIT *p)
{
    return p->rock_gap_com;
}
/*
 * sey compensation for rock gap
 */
void SetRockGapCom(MH_KNIT *p, int n)
{
    p->rock_gap_com = n;
}
/*
 * get compensation for opened roll
 */
int GetRollOpenCom(MH_KNIT *p)
{
    return p->roll_open_com;
}
/*
 * set compensation for opened roll
 */
void SetRollOpenCom(MH_KNIT *p, int n)
{
    p->roll_open_com = n;
}
/*
 * get yarn before
 */
int GetYarnBefore(MH_KNIT *p)
{
    return p->yarn_before;
}
/*
 * set yarn before
 */
void SetYarnBefore(MH_KNIT *p, int n)
{
    p->yarn_before = n;
}

/*
 * get yarn before set
 */
short GetYarnBeforeSet(MH_KNIT *p, int group, int index)
{
    return p->yarn_set[group][index];
}
/*
 * set yarn before
 */
void SetYarnBeforeSet(MH_KNIT *p, int group, int index, short n)
{
    p->yarn_set[group][index] = n;
}
/*
 *
 */
 void YarnPageDataCopy(MH_KNIT *p, int des_page, int src_page)
 {
     memcpy(p->yarn_set[des_page], p->yarn_set[src_page], sizeof(p->yarn_set[0]));
 }
/*
 * get torque for stopped rab
 */
uchar GetRabStopTorque(MH_KNIT *p)
{
    return p->rab_stop_torque;
}
/*
 * set torque for stopped rab
 */
void SetRabStopTorque(MH_KNIT *p, uchar n)
{
    p->rab_stop_torque = n;
}
/*
 * get ret len for machine
 */
uchar GetMacRetLen(MH_KNIT *p)
{
    return p->mac_ret_len;
}
/*
 * set ret len for machine
 */
void SetMacRetLen(MH_KNIT *p, uchar n)
{
    p->mac_ret_len = n;
}
/*
 * get cloth for auto zero
 */
ushort GetAutoZeroCloth(MH_KNIT *p)
{
    return p->auto_zero_cloth;
}
/*
 * set cloth for auto zero
 */
void SetAutoZeroCloth(MH_KNIT *p, ushort n)
{
    p->auto_zero_cloth = n;
}
/*
 * get back sink value for reset
 */
int GetBackSinkResetValue(MH_KNIT *p)
{
    return p->back_sink_reset_value;
}
/*
 * set back sink value for reset
 */
void SetBackSinkResetValue(MH_KNIT *p, int n)
{
    p->back_sink_reset_value = n;
}
/*
 * get front sink value for reset
 */
int GetFrontSinkResetValue(MH_KNIT *p)
{
    return p->front_sink_reset_value;
}
/*
 * set front sink value for reset
 */
void SetFrontSinkResetValue(MH_KNIT *p, int n)
{
    p->front_sink_reset_value = n;
}
/*
* get shake enable
*/
uchar GetShakeEnable(MH_KNIT *p)
{
    return p->shake_enable;
}
/*
*
*/
void SetShakeEnable(MH_KNIT *p, uchar enable)
{
    p->shake_enable = enable;
}
/*
 * get sense of front shake
 */
uchar GetFrontShakeSense(MH_KNIT *p)
{
    return p->front_shake_sense;
}
/*
 * set sense of front shake
 */
void SetFrontShakeSense(MH_KNIT *p, uchar n)
{
    p->front_shake_sense = n;
}
/*
 * get sense of back shake
 */
uchar GetBackShakeSense(MH_KNIT *p)
{
    return p->back_shake_sense;
}
/*
 * set sense of back shake
 */
void SetBackShakeSense(MH_KNIT *p, uchar n)
{
    p->back_shake_sense = n;
}
/*
 * get front safe door enable
 */
uchar GetFrontSafeDoorEnable(MH_KNIT *p)
{
    return p->front_safe_door_enable;
}
/*
 * set front safe door enable
 */
void SetFrontSafeDoorEnable(MH_KNIT *p, uchar n)
{
    p->front_safe_door_enable = n;
    if(p->company == COMPANY_RN)
    {
        p->back_safe_door_enable = n;
    }
}
/*
 * get back safe door enable
 */
uchar GetBackSafeDoorEnable(MH_KNIT *p)
{
    return p->back_safe_door_enable;
}
/*
 * set back safe door enable
 */
void SetBackSafeDoorEnable(MH_KNIT *p, uchar n)
{
    p->back_safe_door_enable = n;
}
/*
 * get rab warn enable
 */
uint GetMacNum(MH_KNIT *p)
{
    return p->mac_num;
}
/*
 * set rab warn enable
 */
void SetMacNum(MH_KNIT *p, uint n)
{
    p->mac_num = n;
}
/*
 * get board enable
 */
uchar GetBoardEnable(MH_KNIT *p)
{
    return p->board_enable;
}
/*
 * set board enable
 */
void SetBoardEnable(MH_KNIT *p, uchar n)
{
    p->board_enable = n;
}
/*
 * get scissor enable
 */
uchar GetScissorEnable(MH_KNIT *p)
{
    return p->scissor_enable;
}
/*
 * set scissor enable
 */
void SetScissorEnable(MH_KNIT *p, uchar n)
{
    p->scissor_enable = n;
}
/*
 * get rab relax for stop
 */
uchar GetStopRabRelax(MH_KNIT *p)
{
    return p->rab_relax_for_stop;
}
/*
*set yarn take mode
*/
void SetYarnTakeMode(MH_KNIT *p, uchar n)
{
    p->yarn_take_mode = n;
}
/*
*get yarn take mode
*/
uchar GetYarnTakeMode(MH_KNIT *p)
{
    return p->yarn_take_mode;
}
/*
*set auto line setting
*/
void SetAutoLineSet(MH_KNIT *p, uchar n)
{
    p->auto_line_set = n;
}
/*
*get auto line setting
*/
uchar GetAutoLineSet(MH_KNIT *p)
{
    return p->auto_line_set;
}
/*
*  set Turning triangle state
*/
void SetReturnState(MH_KNIT *p, uchar n)
{
    p->return_state = n;
}
/*
*get set Turning triangle state
*/
uchar GetReturnState(MH_KNIT *p)
{
    return p->return_state;
}
/*
*  set rab copy enable
*/
void SetRabCopyEnable(MH_KNIT *p, uchar n)
{
    p->copy_rab_enable = n;
}
/*
*get rab copy enable
*/
uchar GetRabCopyEnable(MH_KNIT *p)
{
    return p->copy_rab_enable;
}
/*
 * set rab relax for stop
 */
void SetStopRabRelax(MH_KNIT *p, uchar n)
{
    p->rab_relax_for_stop = n;
}
/*
 *
 */
 void SetProgressData(MH_KNIT *p, uchar percent, char *sc)
 {
     p->progress_percent = percent;
     sprintf(p->grogress_name, "%s", sc);
 }
/*
 *
 */
  void GetProgressData(MH_KNIT *p, uchar *percent, char *sc)
 {
     * percent = p->progress_percent;
     sprintf(sc, "%s", p->grogress_name);
 }
/*
 *
 */
void SetProgressHwnd(MH_KNIT *p, HWND hWnd)
{
    p->pg_hwnd = hWnd;
}
/*
 *
 */
HWND GetProgressHwnd(MH_KNIT *p)
{
    return p->pg_hwnd;
}
/*****************************************************************************
 * all be feedback of running on machine below
 ****************************************************************************/
/*
 * get economy start
 */
ushort GetEconomyStart(MH_KNIT *p)
{

    return p->economy_start;
}
/*
 * get economy end
 */
ushort GetEconomyEnd(MH_KNIT *p)
{
return p->economy_end;
}
/*
 * get economy times
 */
ushort GetEconomyTimes(MH_KNIT *p)
{
    return p->economy_times;
}
/*
 *
 */
void SetEconomyTimes(MH_KNIT *p, ushort times)
{
    p->economy_times = times;
}
/*
 * get economy left
 */
ushort GetEconomyLeft(MH_KNIT *p)
{
    return p->economy_left;
}
/*
 *
 */
ushort GetEconomyListSize(MH_KNIT *p, ushort economy[][3], ushort size)
{
    int i = 0;
    ECONOMY_LIST *q = p->peco_list;
    while(q)
    {
        if(i >= size)
        {
            AddSysLog(p, LOG_TYPE_EXCEED, LOG_EXCEED_ECONOMY_NEST);
            break;
        }
        economy[i][0] = q->node.start;
        economy[i][1] = q->node.end;
        economy[i][2] = q->node.num;
        ++i;
        q = q->next;
    }
    return i;
}
/*
 *
 */
void SetEconomyListSize(MH_KNIT *p, ushort economy[][3], ushort size)
{
    int i = 0;
    while(i < size)
    {
        ECONOMY_LIST *q = calloc(1, sizeof(ECONOMY_LIST));
        q->node.start = economy[size - 1 - i][0];
        q->node.end = economy[size - 1 - i][1];
        q->node.num = economy[size - 1 - i][2];
        q->next = p->peco_list;
        p->peco_list = q;
        ++i;
    }
}

/*
 * get last line of running
 */
ushort GetLastKnitLine(MH_KNIT *p)
{
    return p->last_line + 1;
}
/*
 * set last line of running
 */
void SetLastKnitLine(MH_KNIT *p, ushort line)
{
    p->last_line = line - 1;
}
/*
 * get needle of running
 */
short GetRunNeedle(MH_KNIT *p)
{
    return p->run_feedback.needle_num;
}
/*
 * get state of handle
 */
int GetHandleState(MH_KNIT *p)
{
    return p->run_feedback.bar_state & 0x03;
}
/*
 * get error code of machine
 */
ushort GetRunErrorCode(MH_KNIT *p)
{
    return p->run_feedback.errcode;
}

/*
 * set economy left
 */
void SetEconomyLeft(MH_KNIT *p, ushort n)
{
    p->economy_left = n;
}
/*
 * get single dmbz for n
 */
int GetSingleDmbz(MH_KNIT *p, uint f0b1, uint which)
{
    int ret = 0;
    f0b1 = f0b1 ? 1 : 0;
    if(which < sizeof(p->dmbz_single_com) / sizeof(int))
    {
        ret = p->dmbz_single_com[f0b1][which];
    }
    return ret;
}
/*
 * set single dmbz for which
 */
void SetSingleDmbz(MH_KNIT *p, uint f0b1, uint which, int value)
{
    f0b1 = f0b1 ? 1 : 0;
    if(which < sizeof(p->dmbz_single_com[0]) / sizeof(int))
    {
        p->dmbz_single_com[f0b1][which] = value;
    }
}
/*
 * get single dmbz for n
 */
int GetYarnDmbz(MH_KNIT *p, uint f0b1, uint which)
{
    int ret = 0;
    f0b1 = f0b1 ? 1 : 0;
    if(which < sizeof(p->dmbz_yarn_com) / sizeof(int))
    {
        ret = p->dmbz_yarn_com[f0b1][which];
    }
    return ret;
}
/*
 * set single dmbz for which
 */
void SetYarnDmbz(MH_KNIT *p, uint f0b1, uint which, int value)
{
    f0b1 = f0b1 ? 1 : 0;
    if(which < sizeof(p->dmbz_yarn_com[0]) / sizeof(int))
    {
        p->dmbz_yarn_com[f0b1][which] = value;
    }
}
/*
 * get exchanged/wide yarn for which
 */
uchar GetExchangedYarn(MH_KNIT *p, uint wide, uint which)
{
    uchar ret = 0;
    wide = wide ? 1 : 0;
    if(which < sizeof(p->gauze_exchange[0]))
    {
        ret = p->gauze_exchange[wide][which];
    }
    return ret;
}
/*
 * set exchanged/wide yarn for which
 */
void SetExchangedYarn(MH_KNIT *p, uint wide, uint which, uchar value)
{
    wide = wide ? 1 : 0;
    if(which < sizeof(p->gauze_exchange[0]))
    {
        p->gauze_exchange[wide][which] = value;
    }
}
/*
 * get state of signal light
 */
uchar GetSignLightState(MH_KNIT *p)
{
    return p->light_state;
}
/*
 * set state of signal light
 */
void SetsignalLightState(MH_KNIT *p, uchar n)
{
    p->light_state = n;
}
/*
 * get enable of driver
 */
uchar GetDriverEnable(MH_KNIT *p)
{
    return p->driver_enable;
}
/*
 * set enable of driver
 */
void SetDriverEnable(MH_KNIT *p, uchar n)
{
    p->driver_enable = n;
}
/*
 * get test speed of machine
 */
uchar GetMacTestSpeed(MH_KNIT *p)
{
    return p->mac_test_speed;
}
/*
 * set test speed of machine
 */
void SetMacTestSpeed(MH_KNIT *p, uchar n)
{
    p->mac_test_speed = n;
}
/*
 * get test speed of rab
 */
uchar GetRabTestSpeed(MH_KNIT *p)
{
    return p->rab_test_speed;
}
/*
 * set test speed of rab
 */
void SetRabTestSpeed(MH_KNIT *p, uchar n)
{
    p->rab_test_speed = n;
}
/*
 * get test open of rab
 */
uchar GetRabTestOpen(MH_KNIT *p)
{
    return p->rab_test_open;
}
/*
 * set test open of rab
 */
void SetRabTestOpen(MH_KNIT *p, uchar n)
{
    p->rab_test_open = n;
}
/*
 * get test speed of roll
 */
uchar GetRollTestSpeed(MH_KNIT *p)
{
    return p->roll_test_speed;
}
/*
 * set test speed of roll
 */
void SetRollTestSpeed(MH_KNIT *p, uchar n)
{
    p->roll_test_speed = n;
}
/*
 * get test open of roll
 */
uchar GetRollTestOpen(MH_KNIT *p)
{
    return p->roll_test_open;
}
/*
 * set test open of roll
 */
void SetRollTestOpen(MH_KNIT *p, uchar n)
{
    p->roll_test_open = n;
}
/*
 * get test of left yarn feeder
 */
uchar GetTestLeftFeeder(MH_KNIT *p)
{
    return p->left_feeder_test;
}
/*
 * set test of left yarn feeder
 */
void SetTestLeftFeeder(MH_KNIT *p, uchar n)
{
    p->left_feeder_test = n;
}
/*
 * get test of right yarn feeder
 */
uchar GetTestRightFeeder(MH_KNIT *p, uchar n)
{
    return p->right_feeder_test;
}
/*
 * set test of right yarn feeder
 */
void SetTestRightFeeder(MH_KNIT *p, uchar n)
{
    p->right_feeder_test = n;
}
/*
 * get warn signal of machine
 */
uchar GetMacWarnSign(MH_KNIT *p)
{
    return p->mac_driver_warn_sign;
}
/*
 * set warn signed of machine
 */
void SetMacWarnSign(MH_KNIT *p, uchar n)
{
    p->mac_driver_warn_sign = n;
}
/*
 * get ready signal of machine
 */
uchar GetMacReadySign(MH_KNIT *p)
{
    return p->mac_driver_ready_sign;
}
/*
 * set ready signal of machine
 */
void SetMacReadySign(MH_KNIT *p, uchar n)
{
    p->mac_driver_ready_sign = n;
}
/*
 * get warn signal of rock
 */
uchar GetRockWarnSign(MH_KNIT *p)
{
    return p->rock_driver_warn_sign;
}
/*
 * set warn sign of rock
 */
void SetRockWarnSign(MH_KNIT *p, uchar n)
{
    p->rock_driver_warn_sign = n;
}
/*
 * get ready sign of rock
 */
uchar GetRockReadySign(MH_KNIT *p)
{
    return p->rock_driver_ready_sign;
}
/*
 * set ready sign of rock
 */
void SetRockReadySign(MH_KNIT *p, uchar n)
{
    p->rock_driver_ready_sign = n;
}
/*
 * get quick signal of handle
 */
uchar GetHandleQuickSign(MH_KNIT *p)
{
    return p->handle_quick_sign;
}
/*
 * set quick signal of handle
 */
void SetHandleQuickSign(MH_KNIT *p, uchar n)
{
    p->handle_quick_sign = n;
}
/*
 * get stop signal of handle
 */
uchar GetHandleStopSign(MH_KNIT *p)
{
    return p->handle_stop_sign;
}
/*
 * set stop signal of handle
 */
void SetHandleStopSign(MH_KNIT *p, uchar n)
{
    p->handle_stop_sign = n;
}
/*
 * get slow signal of handle
 */
uchar GetHandleSlowSign(MH_KNIT *p)
{
    return p->handle_slow_sign;
}
/*
 * set slow signal of handle
 */
void SetHandleSlowSign(MH_KNIT *p, uchar n)
{
    p->handle_slow_sign = n;
}
/*
 * get scram signal of handle
 */
uchar GetHandleScramSign(MH_KNIT *p)
{
    return p->handle_scram_sign;
}
/*
 * set scram signal of handle
 */
void SetHandleScramSign(MH_KNIT *p, uchar n)
{
    p->handle_scram_sign = n;
}
/*
 * get forbid signal on left
 */
uchar GetLeftForbidSign(MH_KNIT *p)
{
    return p->left_forbid_sign;
}
/*
 * set forbid signal on left
 */
void SetLeftForbidSign(MH_KNIT *p, uchar n)
{
    p->left_forbid_sign = n;
}
/*
 * get forbid signal on right
 */
uchar GetRightForbidSign(MH_KNIT *p)
{
    return p->right_forbid_sign;
}
/*
 * set forbid signal on right
 */
void SetRightForbidSign(MH_KNIT *p, uchar n)
{
    p->right_forbid_sign = n;
}
/*
 * get forbid signal of rock
 */
uchar GetRockForbidSign(MH_KNIT *p)
{
    return p->rock_forbid_sign;
}
/*
 * set forbid signal of rock
 */
void SetRockForbidSign(MH_KNIT *p, uchar n)
{
    p->rock_forbid_sign = n;
}
/*
 * get zero signal of rock
 */
uchar GetRockZeroSignal(MH_KNIT *p)
{
    return p->rock_zero_sign;
}
/*
 * set zero signal of rock
 */
void SetRockZeroSignal(MH_KNIT *p, uchar n)
{
    p->rock_zero_sign = n;
}
/*
 * get grab signal on left
 */
uchar GetLeftGrabSign(MH_KNIT *p, uchar n)
{
    return p->left_grab_sign;
}
/*
 * set grab signal on left
 */
void SetLeftGrabSign(MH_KNIT *p, uchar n)
{
    p->left_grab_sign = n;
}
/*
 * get grab signal on right
 */
uchar GetRightGrabSign(MH_KNIT *p)
{
    return p->right_grab_sign;
}
/*
 * set grab signal on right
 */
void SetRightGrabSign(MH_KNIT *p, uchar n)
{
    p->right_grab_sign = n;
}
/*
 * get break signal on balcony
 */
uchar GetBalconyBreakSign(MH_KNIT *p)
{
    return p->balcony_break_sign;
}
/*
 * set break signal on balcony
 */
void SetBalconyBreakSign(MH_KNIT *p, uchar n)
{
    p->balcony_break_sign = n;
}
/*
 * get knot signal on balcony
 */
uchar GetBalconyKnotSign(MH_KNIT *p)
{
    return p->balcony_knot_sign;
}
/*
 * set knot signal on balcony
 */
void SetbalconyKnotSign(MH_KNIT *p, uchar n)
{
    p->balcony_knot_sign = n;
}
/*
 * get big knot signal on balcony
 */
uchar GetBalconyBigKnotSign(MH_KNIT *p)
{
    return p->balcony_big_knot_sign;
}
/*
 * set big knot signal on balcony
 */
void SetBalconyBigKnotSign(MH_KNIT *p, uchar n)
{
    p->balcony_big_knot_sign = n;
}
/*
 * get bad signal of drop
 */
uchar GetDropBadSign(MH_KNIT *p)
{
    return p->drop_bad_sign;
}
/*
 * set bad signal of drop
 */
void SetDropBadSign(MH_KNIT *p, uchar n)
{
    p->drop_bad_sign = n;
}
/*
 * get front shake signal
 */
uchar GetFrontShakeSign(MH_KNIT *p)
{
    return p->front_shake_sign;
}
/*
 * set front shake signal
 */
void SetFrontShakeSign(MH_KNIT *p, uchar n)
{
    p->front_shake_sign = n;
}
/*
 * get back shake signal
 */
uchar GetBackShakeSign(MH_KNIT *p)
{
    return p->back_shake_sign;
}
/*
 * set back shake signal
 */
void SetBackShakeSign(MH_KNIT *p, uchar n)
{
    p->back_shake_sign = n;
}
/*
 * get zero signal of needle
 */
uchar GetNeedleZeroSign(MH_KNIT *p)
{
    return p->needle_zero_sign;
}
/*
 * set zero signal of needle
 */
void SetNeedleZeroSign(MH_KNIT *p, uchar n)
{
    p->needle_zero_sign = n;
}
/*
 * get code signal of machine
 */
uchar GetMacCodeSign(MH_KNIT *p)
{
    return p->mac_code_sign;
}
/*
 * set code signal of machine
 */
void SetMacCodeSign(MH_KNIT *p, uchar n)
{
    p->mac_code_sign = n;
}
/*
 * get code signal of rock
 */
uchar GetRockCodeSign(MH_KNIT *p)
{
    return p->rock_code_sign;
}
/*
 * set code signal of rock
 */
void SetRockCodeSign(MH_KNIT *p, uchar n)
{
    p->rock_code_sign = n;
}
/*
 * get key code signal
 */
uchar GetKeyCodeSign(MH_KNIT *p)
{
    return p->key_code_sign;
}
/*
 * set key code signal
 */
void SetKeyCodeSign(MH_KNIT *p, uchar n)
{
    p->key_code_sign = n;
}
/*
 * get back safe door signal on left
 */
uchar GetLeftBackSafeDoorSign(MH_KNIT *p)
{
    return p->left_back_safe_door_sign;
}
/*
 * set back safe door signal on left
 */
void SetLeftBackSafeDoorSign(MH_KNIT *p, uchar n)
{
    p->left_back_safe_door_sign = n;
}
/*
 * get back safe door signal on right
 */
uchar GetRightBackSafeDoorSign(MH_KNIT *p)
{
    return p->right_back_safe_door_sign;
}
/*
 * set back safe door signal on right
 */
void SetRightBackSafeDoorSign(MH_KNIT *p, uchar n)
{
    p->right_back_safe_door_sign = n;
}
/*
 * get warn signal of front rab
 */
uchar GetFrontRabWarnSign(MH_KNIT *p)
{
    return p->front_rab_warn_sign;
}
/*
 * set warn signal of front rab
 */
void SetFrontRabWarnSign(MH_KNIT *p, uchar n)
{
    p->front_rab_warn_sign = n;
}
/*
 * get warn sign of back rab
 */
uchar GetBackRabWarnSign(MH_KNIT *p)
{
    return p->back_rab_warn_sign;
}
/*
 * set warn sign of back rab
 */
void SetBackRabWarnSign(MH_KNIT *p, uchar n)
{
    p->back_rab_warn_sign = n;
}
/*
 * get warn signal of roll
 */
uchar GetRollWarnSign(MH_KNIT *p)
{
    return p->roll_warn_sign;
}
/*
 * set warn signal of roll
 */
void SetRollWarnSign(MH_KNIT *p, uchar n)
{
    p->roll_warn_sign = n;
}
/*
 * get bad signal of roll
 */
uchar GetRollBadSign(MH_KNIT *p)
{
    return p->roll_bad_sign;
}
/*
 * set bad signal of roll
 */
void SetRollBadSign(MH_KNIT *p, uchar n)
{
    p->roll_bad_sign = n;
}
/*
 * get open signal of roll
 */
uchar GetRollOpenSign(MH_KNIT *p)
{
    return p->roll_open_sign;
}
/*
 * set open signal of roll
 */
void SetRollOpenSign(MH_KNIT *p, uchar n)
{
    p->roll_open_sign = n;
}
/*
 * get open signal of rab
 */
uchar GetRabOpenSign(MH_KNIT *p)
{
    return p->rab_open_sign;
}
/*
 * set open signal of rab
 */
void SetRabOpenSign(MH_KNIT *p, uchar n)
{
    p->rab_open_sign = n;
}
/*
 * get feeder signal on left
 */
uchar GetLeftFeederWarnSign(MH_KNIT *p)
{
    return p->left_feeder_warn_sign;
}
/*
 * set feeder signal on left
 */
void SetLeftFeederWarnSign(MH_KNIT *p, uchar n)
{
    p->left_feeder_warn_sign = n;
}
/*
 * get feeder signal on right
 */
uchar GetRightFeederWarnSign(MH_KNIT *p)
{
    return p->right_feeder_warn_sign;
}
/*
 * set feeder signal on right
 */
void SetRightFeederWarnSign(MH_KNIT *p, uchar n)
{
    p->right_feeder_warn_sign = n;
}
/*
 * get speed of rab for sect
 */
char GetRabRunSpeed(MH_KNIT *p, uint sect)
{
    char ret = 0;
    if(sect < sizeof(p->rab_speed))
    {
        ret = p->rab_speed[sect];
    }
    return ret;
}
/*
 * set speed of rab for sect
 */
void SetRabRunSpeed(MH_KNIT *p, uint sect, char speed)
{
    if(sect < sizeof(p->rab_speed))
    {
        p->rab_speed[sect] = speed;
    }
}
/*
 *
 */
int CopyKnitRabSpeed(MH_KNIT *p, char speed)
{
    int ret = 0;
    int i = 0;
    for(i = 0; i < sizeof(p->rab_speed); ++i)
    {
        p->rab_speed[i] = speed;
    }
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            //
        }
    }
    return ret;
}
/*
 * get speed of roll for sect
 */
uchar GetRollRunSpeed(MH_KNIT *p, uint sect)
{
    uchar ret = 0;
    if(sect < sizeof(p->roll_speed))
    {
        ret = p->roll_speed[sect];
    }
    return ret;
}
/*
 * set speed of roll for sect
 */
void SetRollRunSpeed(MH_KNIT *p, uint sect, uchar speed)
{
    if(sect < sizeof(p->roll_speed))
    {
        p->roll_speed[sect] = speed;
    }
}
/*
 *
 */
int CopyKnitRollSpeed(MH_KNIT *p, uchar speed)
{
    int ret = 0;
    int i = 0;
    for(i = 0; i < sizeof(p->roll_speed); ++i)
    {
        p->roll_speed[i] = speed;
    }
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            //
        }
    }
    return ret;
}
/*
 * get speed of switch for sect
 */
uchar GetSwitchRunSpeed(MH_KNIT *p, uint sect)
{
    uchar ret = 0;
    if(sect < sizeof(p->switch_speed))
    {
        ret = p->switch_speed[sect];
    }
    return ret;
}
/*
 * set speed of switch for sect
 */
void SetSwitchRunSpeed(MH_KNIT *p, uint sect, uchar speed)
{
    if(sect < sizeof(p->switch_speed))
    {
        p->switch_speed[sect] = speed;
    }
}
/*
 *
 */
int CopyKnitSwitchSpeed(MH_KNIT *p, uchar speed)
{
    int ret = 0;
    int i = 0;
    for(i = 0; i < sizeof(p->switch_speed); ++i)
    {
        p->switch_speed[i] = speed;
    }
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            //
        }
    }
    return ret;
}
/*
 * get rude of dumu for sect
 * param sect: start from 0 to 31
 */
uchar GetRudeDumu(MH_KNIT *p, uint sect)
{
    short ret = 0;
    if(sect < sizeof(p->rude_dumu) / sizeof(uchar))
    {
        ret = p->rude_dumu[sect];
    }
    return ret;
}
/*
 *
 */
int SetRudeDumu(MH_KNIT *p, uint sect, uchar dumu)
{
    int ret = 0;
    if(sect < sizeof(p->rude_dumu) / sizeof(uchar))
    {
        p->rude_dumu[sect] = dumu;
        if(p->machine_ok)
        {
            if(p->communication == COMM_SRAM)
            {
                RunChangeParam(p);
            }
        }
    }
    return ret;
}
/*
 *
 */
int CopyRudeDumu(MH_KNIT *p, uchar dumu)
{
    int ret = 0;
    int i = 0;
    for(i = 0; i < sizeof(p->rude_dumu) / sizeof(uchar); ++i)
    {
        p->rude_dumu[i] = dumu;
    }
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            //
        }
    }
    return ret;
}
/*
 * get speed of machine for sect
 * param sect: start from 0 to 31
 */
uchar GetMacRunSpeed(MH_KNIT *p, uint sect)
{
    uchar ret = 0;
    if(sect < sizeof(p->mac_speed))
    {
        ret = p->mac_speed[sect];
    }
    return ret;
}
/*
 * set speed of machine for sect
 */
int SetMacRunSpeed(MH_KNIT *p, uint sect, uchar speed)
{
    int ret = 0;
    if(sect < sizeof(p->mac_speed))
    {
        p->mac_speed[sect] = speed;
        if(p->machine_ok)
        {
            if(p->communication == COMM_SRAM)
            {
                RunChangeParam(p);
            }
            else
            {
                ret = MacSetMotorSpeed(p->pmac, sect, speed);
            }
        }
    }
    return ret;
}
/*
 * set speed of machine for sect
 */
int SetMacRunSpeedData(MH_KNIT *p, uint sect, uchar speed)
{
    int ret = 0;
    if(sect < sizeof(p->mac_speed))
    {
        p->mac_speed[sect] = speed;
    }
    return ret;
}
/*
 *
 */
int CopyKnitSpeed(MH_KNIT *p, uchar speed)
{
    int ret = 0;
    int i = 0;
    for(i = 0; i < sizeof(p->mac_speed); ++i)
    {
        p->mac_speed[i] = speed;
    }
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            //
        }
    }
    return ret;
}
/*
 * get speed of yarn feeder
 * param sect: start from 0 to 31
 */
uchar GetFeederRunSpeed(MH_KNIT *p, uint sect)
{
    uchar ret = 0;
    if(sect < sizeof(p->feeder_speed))
    {
        ret = p->feeder_speed[sect];
    }
    return ret;
}
/*
 * set speed of yarn feeder
 */
int SetFeederRunSpeed(MH_KNIT *p, uint sect, uchar speed)
{
    int ret = 0;
    if(sect < sizeof(p->feeder_speed))
    {
        p->feeder_speed[sect] = speed;
    }
    return ret;
}
/*
 * get which dumu for sect
 * param sect: start from 0-31
 */
ushort GetRunDumu(MH_KNIT *p, uint f0b1, uint sect, uint which)
{
    ushort ret = 0;
    f0b1 = f0b1 ? 1 : 0;
    if(sect < sizeof(p->dumu_value[0][0]) / sizeof(ushort))
    {
        if(which < sizeof(p->dumu_value[0]) / sizeof(p->dumu_value[0][0]))
        {
            ret = p->dumu_value[f0b1][which][sect];
        }
    }
    return ret;
}
/*
 * set which dumu for sect
 */
 void SetRunDumuData(MH_KNIT *p, uint f0b1, uint sect, uint which, ushort dumu)
 {
    f0b1 = f0b1 ? 1 : 0;
    if(sect < sizeof(p->dumu_value[0][0]) / sizeof(ushort))
    {
        if(which < sizeof(p->dumu_value[0]) / sizeof(p->dumu_value[0][0]))
        {
            p->dumu_value[f0b1][which][sect] = dumu;
        }
    }
 }
/*
 * set which dumu for sect
 */
int SetRunDumu(MH_KNIT *p, uint f0b1, uint sect, uint which, ushort dumu)
{
    int ret = -1;
    f0b1 = f0b1 ? 1 : 0;
    if(sect < sizeof(p->dumu_value[0][0]) / sizeof(ushort))
    {
        if(which < sizeof(p->dumu_value[0]) / sizeof(p->dumu_value[0][0]))
        {
            p->dumu_value[f0b1][which][sect] = dumu;
        }
        if(p->machine_ok)
        {
            if(p->communication == COMM_SRAM)
            {
                RunChangeParam(p);
            }
            else
            {
                ret = MacSetDumu(p->pmac, f0b1, which, sect, dumu);
            }
        }
    }
    return ret;
}
/*
 *
 */
int CopyKnitDumu(MH_KNIT *p, uint f0b1, uint sect, ushort dumu)
{
    int ret = 0;
    int i = 0;
    f0b1 = f0b1 ? 1 : 0;
    for(i = 0; i < 8; ++i)
    {
        p->dumu_value[f0b1][i][sect] = dumu;
        if(p->communication != COMM_SRAM)
        {
            ret = MacSetDumu(p->pmac, f0b1, i, sect, dumu);
        }
    }
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
    }
    return ret;
}
/*
 * get which yarn stop for sect
 */
uchar GetRunYarnStop(MH_KNIT *p, uint l0r1, uint sect, uint which)
{
    uchar ret = 0;
    l0r1 = l0r1 ? 1 : 0;
    if(sect < sizeof(p->yarn_stop[0]) / sizeof(p->yarn_stop[0][0]))
    {
        if(which < sizeof(p->yarn_stop[0][0]))
        {
            ret = p->yarn_stop[l0r1][sect][which];
        }
    }
    return ret;
}
/*
 * set which yarn stop for sect
 */
int SetRunYarnStop(MH_KNIT *p, uint l0r1, uint sect, uint which, uchar stop)
{
    int ret = -1;
    l0r1 = l0r1 ? 1 : 0;
    if(sect < sizeof(p->yarn_stop[0]) / sizeof(p->yarn_stop[0][0]))
    {
        if(which < sizeof(p->yarn_stop[0][0]))
        {
            p->yarn_stop[l0r1][sect][which] = stop;
            if(p->machine_ok)
            {
                if(p->communication == COMM_SRAM)
                {
                    RunChangeParam(p);
                }
                else
                {
                    ret = MacSetYarnStop(p->pmac, l0r1, which, sect, stop);
                }
            }
        }
    }
    return ret;
}
/*
 * get which sink for sect
 */
ushort GetRunSink(MH_KNIT *p, uint sect, uint which)
{
    ushort ret = 0;
    if(sect < sizeof(p->sink_value) / sizeof(p->sink_value[0]))
    {
        if(which < sizeof(p->sink_value[0]) / sizeof(ushort))
        {
            ret = p->sink_value[sect][which];
        }
    }
    return ret;
}
/*
 * get front sink zero
 */
 ushort GetFrontSinkZero(MH_KNIT *p)
 {
     return p->front_sink_reset_value;
 }
 /*
 * Set front sink zero
 */
 void SetFrontSinkZero(MH_KNIT *p, ushort data)
 {
     p->front_sink_reset_value = data;
 }
 /*
 * get Back sink zero
 */
 ushort GetBackSinkZero(MH_KNIT *p)
 {
     return p->back_sink_reset_value;
 }
 /*
 * Set Back sink zero
 */
 void SetBackSinkZero(MH_KNIT *p, ushort data)
 {
     p->back_sink_reset_value = data;
 }
  /*
 * Set sink window foucs vice
 */
 void SetSinkFocus(MH_KNIT *p, int  focus)
 {
     p->sink_focus = focus;
 }
   /*
 * Get sink window foucs vice
 */
 int GetSinkFocus(MH_KNIT *p)
 {
     return p->sink_focus;
 }
/*
 * Set sink window foucs vice
 */
 void SetCamFocus(MH_KNIT *p, int  focus)
 {
     p->cam_focus = focus;
 }
   /*
 * Get cam window foucs
 */
 int GetCamFocus(MH_KNIT *p)
 {
     return p->cam_focus;
 }
/*
 * set which sink for sect
 */
void SetRunSink(MH_KNIT *p, uint sect, uint which, ushort sink)
{
    if(sect < sizeof(p->sink_value) / sizeof(p->sink_value[0]))
    {
        if(which < sizeof(p->sink_value[0]) / sizeof(ushort))
        {
            p->sink_value[sect][which] = sink;
        }
    }
}
/*
 * get yarn stop compensation for each speed
 * param l0r1: (0-from right to left 1-from left to right)
 */
short GetYarnStopCom(MH_KNIT *p, uint l0r1, uint which)
{
    short ret = 0;
    if(l0r1 < sizeof(p->yarn_before_com) / sizeof(p->yarn_before_com[0]))
    {
        if(which < sizeof(p->yarn_before_com[0][0]) / sizeof(short))
        {
            ret = p->yarn_before_com[l0r1][0][which];
        }
    }
    return ret;
}
/*
 * set yarn stop compensation for each speed
 * param l0r1: (0-from right to left 1-from left to right)
 */
void SetYarnStopCom(MH_KNIT *p, uint l0r1, uint which, short com)
{
    if(l0r1 < sizeof(p->yarn_before_com) / sizeof(p->yarn_before_com[0]))
    {
        if(which < sizeof(p->yarn_before_com[0][0]) / sizeof(short))
        {
            p->yarn_before_com[l0r1][0][which] = com;
        }
    }
}
/*
 * get yarn before for each speed
 */
short GetYarnBeforeCom(MH_KNIT *p, uint l0r1, uint which)
{
    short ret = 0;
    if(l0r1 < sizeof(p->yarn_before_com) / sizeof(p->yarn_before_com[0]))
    {
        if(which < sizeof(p->yarn_before_com[0][1]) / sizeof(short))
        {
            ret = p->yarn_before_com[l0r1][1][which];
        }
    }
    return ret;
}
/*
 * set yarn before for each speed
 * do not return if fail for dec alarm
 */
void SetYarnBeforeCom(MH_KNIT *p, uint l0r1, uint which, short com)
{
    if(l0r1 < sizeof(p->yarn_before_com) / sizeof(p->yarn_before_com[0]))
    {
        if(which < sizeof(p->yarn_before_com[0][1]) / sizeof(short))
        {
            p->yarn_before_com[l0r1][1][which] = com;
        }
    }
}
/*
 * get mac before for each speed
 */
short GetMacBeforeCom(MH_KNIT *p, uint l0r1, uint which)
{
    short ret = 0;
    if(l0r1 < sizeof(p->mac_before_com) / sizeof(p->mac_before_com[0]))
    {
        if(which < sizeof(p->mac_before_com[0]) / sizeof(short))
        {
            ret = p->mac_before_com[l0r1][which];
        }
    }
    return ret;
}
/*
 * set mac before for each speed
 */
void SetMacBeforeCom(MH_KNIT *p, uint l0r1, uint which, short com)
{
    if(l0r1 < sizeof(p->mac_before_com) / sizeof(p->mac_before_com[0]))
    {
        if(which < sizeof(p->mac_before_com[0]) / sizeof(short))
        {
            p->mac_before_com[l0r1][which] = com;
        }
    }
}
/*
 * get dumu before for each speed
 */
short GetDumuBeforeCom(MH_KNIT *p, uint n0t1, uint which)
{
    short ret = 0;
    if(n0t1 < sizeof(p->dumu_before_com) / sizeof(p->dumu_before_com[0]))
    {
        if(which < sizeof(p->dumu_before_com[0]) / sizeof(short))
        {
            ret = p->dumu_before_com[n0t1][which];
        }
    }
    return ret;
}
/*
 * set dumu before for each speed
 */
void SetDumuBeforeCom(MH_KNIT *p, uint n0t1, uint which, short com)
{
    if(n0t1 < sizeof(p->dumu_before_com) / sizeof(p->dumu_before_com[0]))
    {
        if(which < sizeof(p->dumu_before_com[0]) / sizeof(short))
        {
            p->dumu_before_com[n0t1][which] = com;
        }
    }
}
/*
 * get number of extent piece
 */
uchar GetExtPieceNum(MH_KNIT *p)
{
    return p->ext_piece_num;
}
/*
 *
 */
void SetExtPieceNum(MH_KNIT *p, uchar n)
{
    p->ext_piece_num = n;
}
/*
 * get inter of extent piece
 */
ushort GetExtPieceInter(MH_KNIT *p)
{
    return p->ext_piece_inter;
}
/*
 * set inter of extent piece
 */
void SetExtPieceInter(MH_KNIT *p, ushort inter)
{
    p->ext_piece_inter = inter;
}
/*
 * get width of extent piece
 */
ushort GetExtPieceWidth(MH_KNIT *p)
{
    return p->ext_piece_width;
}
/*
 * set width of extent piece
 */
void SetExtPieceWidth(MH_KNIT *p, ushort width)
{
    p->ext_piece_width = width;
}
/*
 * get which enable of extent piece
 */
uchar GetExtPieceEnable(MH_KNIT *p, uint sect)
{
    uchar ret = 0;
    if(sect < sizeof(p->ext_piece_enable))
    {
        ret = p->ext_piece_enable[sect];
    }
    return ret;
}
/*
 * set which enable of extent piece
 */
void SetExtPieceEnable(MH_KNIT *p, uint sect, uchar enable)
{
    if(sect < sizeof(p->ext_piece_enable))
    {
        p->ext_piece_enable[sect] = enable;
    }
}
/*
 * get paired yarn of extent piece for sect
 * left: 1-8; right: 11-18
 */
uchar GetExtPieceYarn(MH_KNIT *p, uint sect, uint which)
{
    uchar ret = 0;
    if(sect < sizeof(p->ext_piece_yarn) / sizeof(p->ext_piece_yarn[0]))
    {
        if(which < sizeof(p->ext_piece_yarn[0]))
        {
            ret = p->ext_piece_yarn[sect][which];
        }
    }
    return ret;
}
/*
 * set paired yarn of extent piece for sect
 * left: 1-8; right: 11-18
 */
void SetExtPieceYarn(MH_KNIT *p, uint sect, uint which, uchar yarn)
{
    if(sect < sizeof(p->ext_piece_yarn) / sizeof(p->ext_piece_yarn[0]))
    {
        if(which < sizeof(p->ext_piece_yarn[0]))
        {
            p->ext_piece_yarn[sect][which] = yarn;
        }
    }
}
/*
 * get feeder speed for sect
 */
uchar GetYarnFeederSpeed(MH_KNIT *p, uint sect)
{
    uchar ret = 0;
    if(sect < sizeof(p->feeder_speed))
    {
        ret = p->feeder_speed[sect];
    }
    return ret;
}
/*
 * set feeder speed for sect
 */
void SetYarnFeederSpeed(MH_KNIT *p, uint sect, uchar speed)
{
    if(sect < sizeof(p->feeder_speed))
    {
        p->feeder_speed[sect] = speed;
        if(p->machine_ok)
        {
            if(p->communication == COMM_SRAM)
            {
                RunChangeParam(p);
            }
        }
    }
}
/*
 *
 */
void CopyKnitFeederSpeed(MH_KNIT *p, uchar speed)
{
    int i = 0;
    for(i = 0; i < sizeof(p->feeder_speed); ++i)
    {
        p->feeder_speed[i] = speed;
    }
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
    }
}
/*
 * get enable of yarn feeder on left
 */
uchar GetLeftYarnFeederEnable(MH_KNIT *p)
{
    return p->left_feeder_enable;
}
/*
 * set enable of yarn feeder on left
 */
void SetLeftYarnFeederEnable(MH_KNIT *p, uchar enable)
{
    p->left_feeder_enable = enable;
}
/*
 * get enable of yarn feeder on right
 */
uchar GetRightYarnFeederEnable(MH_KNIT *p)
{
    return p->right_feeder_enable;
}
/*
 * set enable of yarn feeder on right
 */
void SetRightYarnFeederEnable(MH_KNIT *p, uchar enable)
{
    p->right_feeder_enable = enable;
}
/*
 * get line which to start;
 */
ushort GetLineToStart(MH_KNIT *p)
{
    return p->goto_line;
}
/*
 * set line which to start
 */
void SetLineToStart(MH_KNIT *p, ushort n)
{
    if(n > 0 && n <= p->total_line)
    {
        p->goto_line = n;
        //ResetMachine(p, n, 0);
    }
}
/*
 * get line which to stop
 */
ushort GetStopLine(MH_KNIT *p)
{
    return p->stop_line;
}
/*
 * set line which to stop
 */
int SetStopLine(MH_KNIT *p, ushort n)
{
    int ret = 0;
    p->stop_line = n;
    if(p->machine_ok)
    {
        p->knit_state.stop_line = n ? TRUE : FALSE;
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            ret = MacSendStopLine(p->pmac, n);
        }
    }
    return ret;
}
/*
 * get piece lock
 */
uchar GetPieceLock(MH_KNIT *p)
{
    return p->piece_lock;
}
/*
 * set piece lock
 */
int SetPieceLock(MH_KNIT *p, uchar n)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            ret = MacSendPieceLock(p->pmac, n);
        }
    }
    if(!ret)
    {
        p->piece_lock = n;
    }
    return ret;
}
/*
 * get yarn up
 */
uchar GetYarnUp(MH_KNIT *p)
{
    return p->yarn_up;
}
/*
 * set yarn up
 */
void SetYarnUp(MH_KNIT *p, uchar n)
{
    p->yarn_up = n;
}
/*
 * get running type of machine
 */
uchar GetGuiSpeedType(MH_KNIT *p)
{
    return p->gui_speed_type;
}
/*
 * set running type of machine
 */
void SetGuiSpeedType(MH_KNIT *p, uchar n)
{
    p->gui_speed_type = n;
}
/*
 * get enable of hangding
 */
uchar GetHangDingEnable(MH_KNIT *p)
{
    return p->hangding_enable;
}
/*
 * set enable of hangding
 */
void SetHangDingEnable(MH_KNIT *p, uchar enable)
{
    p->hangding_enable = enable;
}
/*
 * get hangding times
 */
uchar GetHangDingTimes(MH_KNIT *p)
{
    return p->hangding_times;
}
/*
 * set hangding times
 */
void SetHangDingTimes(MH_KNIT *p, uchar times)
{
    p->hangding_times = times;
}
/*
 * get start of hangding
 */
uchar GetHangDingStart(MH_KNIT *p)
{
    return p->hangding_start;
}
/*
 * set start of hangding
 */
void SetHangDingStart(MH_KNIT *p, uchar start)
{
    p->hangding_start = start;
}
/*
 * get end of hangding
 */
uchar GetHangDingEnd(MH_KNIT *p)
{
    return p->hangding_end;
}
/*
 * set end of hangding
 */
void SetHangDingEnd(MH_KNIT *p, uchar end)
{
    p->hangding_end = end;
}
/*
 *
 */
int GetAutoHdDropEnable(MH_KNIT *p)
{
    return p->knit_state.auto_hd_drop;
}
/*
 *
 */
void SetAutoHdDropEnable(MH_KNIT *p, int enable)
{
    p->knit_state.auto_hd_drop = enable;
}
/*
 *
 */
int GetRockZeroDrt(MH_KNIT *p)
{
    return p->knit_state.rock_zero_drt;
}
/*
 *
 */
void SetRockZeroDrt(MH_KNIT *p, int drt)
{
    p->knit_state.rock_zero_drt = drt;
}
/*
 *
 */
int GetTakeYarnMode(MH_KNIT *p)
{
    return p->knit_state.take_yarn_mode;
}
/*
 *
 */
void SetTakeYarnMode(MH_KNIT *p, int mode)
{
    p->knit_state.take_yarn_mode = mode;
}
/*
 *
 */
short GetDumuMaxValue(MH_KNIT *p)
{
    return p->dumu_max_value;
}
/*
 *
 */
void SetDumuMaxValue(MH_KNIT *p, short dumu)
{
    p->dumu_max_value = dumu;
}
/*
 *
 */
short GetDumu1DistanceToMacLeft(MH_KNIT *p)
{
    return p->dumu1_to_mac_left_len;
}
/*
 *
 */
void SetDumu1DistanceToMacLeft(MH_KNIT *p, short n)
{
    p->dumu1_to_mac_left_len = n;
}
/*
 *
 */
short GetDumu2DistanceToMacLeft(MH_KNIT *p)
{
    return p->dumu2_to_mac_left_len;
}
/*
 *
 */
void SetDumu2DistanceToMacLeft(MH_KNIT *p, short n)
{
    p->dumu2_to_mac_left_len = n;
}
/*
 *
 */
short GetDumu3DistanceToMacLeft(MH_KNIT *p)
{
    return p->dumu3_to_mac_left_len;
}
/*
 *
 */
void SetDumu3DistanceToMacLeft(MH_KNIT *p, short n)
{
    p->dumu3_to_mac_left_len = n;
}
/*
 *
 */
short GetDumu4DistanceToMacLeft(MH_KNIT *p)
{
    return p->dumu4_to_mac_left_len;
}
/*
 *
 */
void SetDumu4DistanceToMacLeft(MH_KNIT *p, short n)
{
    p->dumu4_to_mac_left_len = n;
}
/*
 *
 */
int GetSinkDrt(MH_KNIT *p)
{
    return p->knit_state.sink_drt;
}
/*
 *
 */
void SetSinkDrt(MH_KNIT *p, int drt)
{
    p->knit_state.sink_drt = drt;
}
/*
 * get enable of warning
 */
uchar GetWarnEnable(MH_KNIT *p)
{
    return p->warn_enable;
}
/*
 * set enable of warning
 */
void SetWarnEnable(MH_KNIT *p, uchar enable)
{
    p->warn_enable = enable;
}
/*
 * get rab speed for machine stopped
 */
uchar GetRabSpeedForMacStop(MH_KNIT *p)
{
    return p->rab_speed_for_stop;
}
/*
 * set rab speed for machine stopped
 */
void SetRabSpeedForMacStop(MH_KNIT *p, uchar n)
{
    p->rab_speed_for_stop = n;
}
/*
 * get roll speed for machine stopped
 */
uchar GetRollSpeedForMacStop(MH_KNIT *p)
{
    return p->roll_speed_for_stop;
}
/*
 * set roll speed for machine stopped
 */
void SetRollSpeedForMacStop(MH_KNIT *p, uchar n)
{
    p->roll_speed_for_stop = n;
}
/*
 * get switch speed for machine stopped
 */
uchar GetSwitchSpeedForMacStop(MH_KNIT *p)
{
    return p->switch_speed_for_stop;
}
/*
 * set switch speed for machine stopped
 */
void SetSwitchSpeedForMacStop(MH_KNIT *p, uchar n)
{
    p->switch_speed_for_stop = n;
}
///////////////////////////////mac_test/////////////////////////////////
/*
 *
 */
void CarriageDateInit(MH_KNIT *p, int handle, int dm_data, int sk_data)
{
    p->crg = MacCarriageNew(p->pmac, handle);
    carriage_setting_pos(p->crg, sk_data, dm_data);
    carriage_set_sink_enable(p->crg, TRUE);
}
/*
 *
 */
void SinkSettingInit(MH_KNIT *p)
{
    p->sink_set = MacSinkSettingNew(p->pmac);
}
/*
 *
 */
void CamSettingInit(MH_KNIT *p)
{
    p->cam_set = MacCamSettingNew(p->pmac);
}
/*
 *
 */
CARRIAGE* GetCarrigeData(MH_KNIT *p)
{
    return p->crg;
}
/*
 *
 */
 SINK_SETTING * GetSinkSettingData(MH_KNIT *p)
 {
     return p->sink_set;
 }
 /*
 *
 */
 CAM_SETTING * GetCamSettingData(MH_KNIT *p)
 {
     return p->cam_set;
 }
/*
 *
 */
int GetCarrigeResetAll(MH_KNIT *p)
{
    int reset = carriage_get_reset_all(p->crg);
    return reset;
}
/*
 *
 */
int GetMacFocusId(MH_KNIT *p)
{
    return p->mac_focus;
}
/*
 *
 */
void SetMacFocusId(MH_KNIT *p, int n)
{
    p->mac_focus = n;
}

/*
 * reset select
 */
int ResetSelect(MH_KNIT *p, int index)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramResetSelect(p->pmac, index + 200);
        }
        else
        {
            uchar data[4] = { 0 };
            data[0] = 1 << index;
            data[1] = 1 << index;
            data[2] = 1 << index;
            data[3] = 1 << index;
            ret = MacResetSelect(p->pmac, data, sizeof(data));
        }
        if(!ret)
        {
            p->select_state[0][0][index] = 0;
            p->select_state[0][1][index] = 0;
            p->select_state[1][0][index] = 0;
            p->select_state[1][1][index] = 0;
        }
    }
    return ret;
}
/*
 * toggle select of all
 * param index: index of piece
 * param which: which selector, low for front, high for back
 */
int ToggleAllSelect(MH_KNIT *p, int index, int which)
{
    int ret = 0;
    short front = which & 0xFFFF;
    short back = (which >> 16) & 0xFFFF;
    if(p->machine_ok)
    {
        int state = p->select_state[0][0][index];
        if(p->communication == COMM_SRAM)
        {
            ret = SramToggleSelect(p->pmac, 200 + index, !state);
        }
        else
        {
            uchar data[2] = { 0 };
            data[0] = (((front & 0x01) ? state : !state) << 7) | (index << 4);
            data[0] |= (((back & 0x01) ? state : !state) << 3) | index;
            data[1] = (((front & 0x02) ? state : !state) << 7) | (index << 4);
            data[1] |= (((back & 0x02) ? state : !state) << 3) | index;
            ret = MacToggleSelect(p->pmac, data, sizeof(data));
        }
        if(!ret)
        {
            if(front & 0x01)
            {
                p->select_state[0][0][index] = !state;
            }
            if(front & 0x02)
            {
                p->select_state[0][1][index] = !state;
            }
            if(back & 0x01)
            {
                p->select_state[1][0][index] = !state;
            }
            if(back & 0x02)
            {
                p->select_state[1][1][index] = !state;
            }
        }
    }
    return ret;
}
/*
 * toggle the given select
 * param f0b1: 0-front, 1-back
 * param group: 0-3(7) from left to right
 * param index: 0-7(5) from bottom to top
 * return: 0=success, -1=failed
 */
int ToggleSelect(MH_KNIT *p, int f0b1, int group, int index)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            uchar state = !p->select_state[f0b1][group][index];
            int base[2][4] = {{32, 40, 48, 56}, {0, 8, 16, 24}};
            ret = SramToggleSelect(p->pmac, base[f0b1][group] + index, state);
        }
        else
        {
            int i = 0;
            uchar xzq[2][8][8];
            for(i = 0; i < 8; ++i)
            {
                xzq[0][0][i] = p->select_state[0][0][i] ^ 1;
                xzq[1][0][i] = p->select_state[1][0][i] ^ 1;
                xzq[0][1][i] = p->select_state[0][1][i] ^ 1;
                xzq[1][1][i] = p->select_state[1][1][i] ^ 1;

                xzq[0][2][i] = p->select_state[0][2][i] ^ 1;
                xzq[1][2][i] = p->select_state[1][2][i] ^ 1;
                xzq[0][3][i] = p->select_state[0][3][i] ^ 1;
                xzq[1][3][i] = p->select_state[1][3][i] ^ 1;
            }
            xzq[f0b1][group][index] = !xzq[f0b1][group][index];
            uchar data[4] = { 0 };
            data[0] = (xzq[0][0][index] << 7) | (index << 4);
            data[0] |= (xzq[1][0][index] << 3) | index;
            data[1] = (xzq[0][1][index] << 7) | (index << 4);
            data[1] |= (xzq[1][1][index] << 3) | index;

            data[2] = (xzq[0][2][index] << 7) | (index << 4);
            data[2] |= (xzq[1][2][index] << 3) | index;
            data[3] = (xzq[0][3][index] << 7) | (index << 4);
            data[3] |= (xzq[1][3][index] << 3) | index;
            ret = MacToggleSelect(p->pmac, data, sizeof(data));
        }
        if(!ret)
        {
            p->select_state[f0b1][group][index] = !p->select_state[f0b1][group][index];
        }
    }
    return ret;
}
/*
 * state: 1=down, 0=up
 */
int SelectAct(MH_KNIT *p, int f0b1, int group, int index, int state)
{
    int ret = 0;
    if(p->communication == COMM_SRAM)
    {
        int base[2][4] = { { 32, 40, 48, 56 }, { 0, 8, 16, 24 } };
        ret = SramToggleSelect(p->pmac, base[f0b1][group] + index, state);
    }
    if(!ret)
    {
        p->select_state[f0b1][group][index] = state;
    }
    return ret;
}
/*
 * get state of select
 */
uchar GetSelectState(MH_KNIT *p, int f0b1, int group, int index)
{
    return p->select_state[f0b1][group][index];
}
/*
 * get state of select
 */
void SetSelectState(MH_KNIT *p, int f0b1, int group, int index, uchar data)
{
     p->select_state[f0b1][group][index] = data;
}
/*
 * reset given yarn
 */
int ResetYarn(MH_KNIT *p, int l0r1, int index)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramResetYarn(p->pmac, index);
        }
        else
        {
            uchar data = 0x80 >> index;
            ret = MacResetYarn(p->pmac, &data, sizeof(data));
        }
        if(!ret)
        {
            p->yarn_state[l0r1] &= ~(0x80 >> index);
        }
    }
    return ret;
}
/*
 * toggle the given yarn
 * param l0r1: 0-left, 1-right
 * param index: 0-7 from the first yarn to the eighth yarn
 */
int ToggleYarn(MH_KNIT *p, int l0r1, int index)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            uchar state = p->yarn_state[l0r1] >> (7 - index);
            state &= 1;
            ret = SramToggleYarn(p->pmac, index + l0r1 * 8, !state);
        }
        else
        {
            p->yarn_state[l0r1] ^= 0x80 >> index;    //save MH_KNIT data of yarn_state
            uchar data[2] = {0};
            data[0] = p->yarn_state[0];
            data[1] = p->yarn_state[1];
            ret = MacToggleYarn(p->pmac, data, sizeof(data));
        }
    }
    return ret;
}
/*
 * group: l=0,r=1
 */
int YarnAct(MH_KNIT *p, int group, int index, int state)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramToggleYarn(p->pmac, index + group * 8, state);
        }
        else
        {
            //
        }
        if(!ret)
        {
            p->yarn_state[group] &= ~(0x80 >> index);
            if(state)
            {
                p->yarn_state[group] |= 0x80 >> index;
            }
        }
    }
    return ret;
}
/*
 * get state of yarn
 */
int GetYarnState(MH_KNIT *p, int l0r1, int index)
{
    return (p->yarn_state[l0r1] >> (7 - index)) & 1;
}
/*
 * get state of yarn with one byte
 */
int GetYarn(MH_KNIT *p, int l0r1)
{
    return p->yarn_state[l0r1];
}
/*
 * set state of yarn with one byte
 */
void SetYarn(MH_KNIT *p, int l0r1, uchar data)
{
    p->yarn_state[l0r1] = data;
}
/*
 * reset given cam
 */
int ResetCam(MH_KNIT *p, int f0b1, int group, int index)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            uchar cam[2][2][6] = {
                {{0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 4, 5}},
                {{8, 9, 10, 11, 12, 13}, {8, 9, 10, 11, 12, 13}}
            };
            ret = SramResetCam(p->pmac, cam[f0b1][group][index]);
        }
        else
        {
            uchar data[2] = { 0 };
            data[f0b1] = 1 << index;
            ret = MacResetCam(p->pmac, data, sizeof(data));
        }
        if(!ret)
        {
            p->cam_state[f0b1][group] &= ~(0x01 << index);
        }
    }
    return ret;
}
/*
 * toggle the given cam
 * param f0b1: 0-front, 1- back
 * param group: 0-1(3) from left to right
 * param index: 0-left 2dumu, 1-transfer, 2-right 2dumu,
 *              3-left receive, 4-tuck, 5-right receive
 */
int ToggleCam(MH_KNIT *p, int f0b1, int group, int index)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            int reverse = FALSE;
            uchar item = 0;
            if(p->sys_info.cam == 2)
            {
                uchar cam[2][2][6] = { { { 7, 6, 16, 17, 18, 19 }, { 15, 14,
                        20, 21, 22, 23 } }, { { 3, 5, 4, 0, 1, 2 }, { 11, 13,
                        12, 8, 9, 10 } } };
                item = cam[f0b1][group][index];
                if(index == 1 || index == 4)
                {
                    reverse = TRUE;
                }
            }
            else
            {
                uchar cam[2][2][6] = {
                        { { 0, 1, 2, 3, 4, 5 }, { 0, 1, 2, 3, 4,5 } },
                        { { 8, 9, 10, 11, 12, 13 }, { 8, 9, 10, 11, 12, 13 } }
                };
                item = cam[f0b1][group][index];
                if(index != 4)
                {
                    reverse = TRUE;
                }
            }
            uchar state = p->cam_state[f0b1][group] >> index;
            state &= 1;
            ret = SramToggleCam(p->pmac, item, reverse ? state : !state);
        }
        else
        {
             if(f0b1 || index == 3 || index == 5)
            {
                if(f0b1)
                {
                    p->cam_state[f0b1][group] ^= 0x01 << index;
                }
                else
                {
                    index = index > 4 ? 6 : 7;
                    p->cam_state[1][group] ^= 0x01 << index;
                }

             }
              else
             {
                 uchar num[2][4] = {{1, 2, 3, 0}, {5, 6, 7, 4} };
                 if(index == 4) index = index -1;
                 p->cam_state[0][0] ^= 0x01 << num[group][index];
             }
            uchar data[3] = { 0 };
            data[0] = p->cam_state[1][0] ;
            data[1] = p->cam_state[1][1] ;
            data[2] = p->cam_state[0][0] ;

            ret = MacToggleCam(p->pmac, data, sizeof(data));
        }
    }
    return ret;
}
/*
 *
 */
int CamAct(MH_KNIT *p, int f0b1, int group, int index, int state)
{
    int ret = 0;
    if(p->communication == COMM_SRAM)
    {
        int item = 0;
        int reverse = FALSE;
        if(p->sys_info.cam == 2)
        {
            uchar iron[2][2][6] = {
                    {
                            { 7, 6, 16, 17, 18, 19 },
                            { 15, 14, 20, 21, 22, 23 }
                    },
                    {
                            { 3, 5, 4, 0, 1, 2 },
                            { 11, 13, 12, 8, 9, 10 }
                    }
            };
            item = iron[f0b1][group][index];
            if((index % 3) == 1)
            {
                reverse = TRUE;
            }
        }
        else
        {
             uchar iron[2][2][6] = {
                        {{0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 4, 5}},
                        {{8, 9, 10, 11, 12, 13}, {8, 9, 10, 11, 12, 13}}
             };
             item = iron[f0b1][group][index];
        }
        ret = SramToggleCam(p->pmac, item, reverse ? !state : state);
    }
    if(!ret)
    {
        p->cam_state[f0b1][group] &= ~(0x01 << index);
        if(state)
        {
            p->cam_state[f0b1][group] |= 0x01 << index;
        }
    }
    return ret;
}
/*
 * get state of cam
 */
int GetCamState(MH_KNIT *p, int f0b1, int group, int index)
{
    int ret = 0;
    ret = (p->cam_state[f0b1][group] >> index) & 1;
    return ret;
}
/*
 * get state of cam setting
 */
int GetCamSettingState(MH_KNIT *p, uint sect, uint which)
{
    ushort ret = 0;
    if(sect < sizeof(p->cam_value) / sizeof(p->cam_value[0]))
    {
        if(which < sizeof(p->cam_value[0]) / sizeof(ushort))
        {
            ret = p->cam_value[sect][which];
        }
    }
    return ret;
}
/*
 * set which cam for sect
 */
void SetCamSettingState(MH_KNIT *p, uint sect, uint which, ushort cam)
{
    if(sect < sizeof(p->cam_value) / sizeof(p->cam_value[0]))
    {
        if(which < sizeof(p->cam_value[0]) / sizeof(ushort))
        {
            p->cam_value[sect][which] = cam;
        }
    }
}

/*
 * return: >0 没有动作
 */
#define STITCH_1FRONT             0x00000001
#define STITCH_2FRONT             0x00000002
#define STITCH_3FRONT             0x00000004
#define STITCH_4FRONT             0x00000008
#define STITCH_1BACK              0x00010000
#define STITCH_2BACK              0x00020000
#define STITCH_3BACK              0x00040000
#define STITCH_4BACK              0x00080000

#define FORWARD     0x2000
#define REVERSE     0x0000

///////////////////////////////////////////////////


/*
 * reset dumu
 */
int ResetDumu(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramToggleStitch(p->pmac, 244, 0);
        }
        else
        {
            short data[4] = { 0 };
            short cmd = 0xC000 | 800;
            data[0] = cmd;
            if(p->sys_info.motor & 0x01)
            {
                data[0] |= FORWARD;
            }
            data[1] = cmd;
            if(p->sys_info.motor & 0x02)
            {
                data[1] |= FORWARD;
            }
            data[2] = cmd;
            if(p->sys_info.motor & 0x04)
            {
                data[2] |= FORWARD;
            }
            data[3] = cmd;
            if(p->sys_info.motor & 0x08)
            {
                data[3] |= FORWARD;
            }
//            ret = MacToggleStitch(p->pmac, (uchar*) data, sizeof(data));
    }
        if(!ret)
        {
            p->dumu_state[0][0] = 0;
            p->dumu_state[0][1] = 0;
            p->dumu_state[1][0] = 0;
            p->dumu_state[1][1] = 0;
        }
    }
    return ret;
}
/*
 * get state of dumu
 */
short GetDumuState(MH_KNIT *p, int f0b1, int index)
{
    return p->dumu_state[f0b1][index];
}
/*
 * get sensor of dumu
 */
char GetDumuSensor(MH_KNIT *p, int f0b1, int index)
{
    return p->dumu_sensor[f0b1*4 +index];
}
/*
 * get sensor of dumu
 */
int GetSinkSensor(MH_KNIT *p, int index)
{
    return p->sink_sensor[index];
}
#define SINK_1FRONT              0x00000001
#define SINK_1BACK               0x00010000
/*
 *
 */
int ToggleSink(MH_KNIT *p, int bit, short value[2][4])
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            uchar item = 0;
            short state = 0;
            switch (bit)
            {
            case SINK_1FRONT:
                item = 0;
                if(!value[0][0])
                {
                    state = 0;
                }
                else if(value[0][0] == p->sink_state[0][0])
                {
                    return TRUE;
                }
                else
                {
                    state = value[0][0] - p->sink_state[0][0];
                }
                break;
            case SINK_1BACK:
                item = 1;
                if(!value[1][0])
                {
                    state = 0;
                }
                else if(value[1][0] == p->sink_state[1][0])
                {
                    return TRUE;
                }
                else
                {
                    state = value[1][0] - p->sink_state[1][0];
                }
                break;
            }
            ret = SramToggleSink(p->pmac, item, state);
        }
        if(!ret)
        {
            if(bit & SINK_1FRONT)
            {
                p->sink_state[0][0] = value[0][0];
            }
            if(bit & SINK_1BACK)
            {
                p->sink_state[1][0] = value[1][0];
            }
        }
    }
    return ret;
}
/*
 *
 */
struct SRC_2HEAD_FB {
    uint sink_b : 1;
    uint dumu_b4 : 1;
    uint dumu_b3 : 1;
    uint dumu_f4 : 1;
    uint dumu_f3 : 1;
    uint sink_f : 1;
    uint dumu_b2 : 1;
    uint dumu_b1 : 1;
    uint dumu_f2 : 1;
    uint dumu_f1 : 1;
    uint yarn : 1;
    uint cam : 1;
    uint select : 1;
    uint probe_b : 1;
    uint probe_f : 1;
    uint r2l : 1;
    uint l2r : 1;
    uint yarn_24ac : 1;
    uint select_n24ac : 1;
    uint select_p24ac : 1;
    uint dumu_24ac : 1;
    uint rsvd : 10;
    uint can : 1;
};
/*
 *
 */
struct DES_HEAD_FB {
    uint sink_b : 1;
    uint sink_f : 1;
    uint dumu_b2 : 1;
    uint dumu_b1 : 1;
    uint dumu_f2 : 1;
    uint dumu_f1 : 1;
    uint dumu_b4 : 1;
    uint dumu_b3 : 1;
    uint dumu_f4 : 1;
    uint dumu_f3 : 1;
    uint yarn : 1;
    uint cam : 1;
    uint select : 1;
    uint rsvd : 18;
    uint can : 1;
};
void ConvertHeadFeedback(const struct SRC_2HEAD_FB *psrc, struct DES_HEAD_FB *pdes)
{
    pdes->yarn = psrc->yarn;
    pdes->select = psrc->select;
    pdes->cam = psrc->cam;
    pdes->can = psrc->can;
    pdes->dumu_b1 = psrc->dumu_b1;
    pdes->dumu_b2 = psrc->dumu_b2;
    pdes->dumu_b3 = psrc->dumu_b3;
    pdes->dumu_b4 = psrc->dumu_b4;
    pdes->dumu_f1 = psrc->dumu_f1;
    pdes->dumu_f2 = psrc->dumu_f2;
    pdes->dumu_f3 = psrc->dumu_f3;
    pdes->dumu_f4 = psrc->dumu_f4;
}
/*
 * get state of machine header
 */
int GetHeadTestState(MH_KNIT *p, HEAD_TEST *phead_test)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            int feedback = 0;
            ret = SramGetHeaderFeedback(p->pmac, &feedback);
            if(!ret)
            {
                if(p->sys_info.cam == 2)
                {
                    struct SRC_2HEAD_FB *psrc = (struct SRC_2HEAD_FB *)&feedback;
                    struct DES_HEAD_FB *pdes = (struct DES_HEAD_FB *)&phead_test->error;
                    ConvertHeadFeedback(psrc, pdes);
                }
                else
                {
                    phead_test->error = feedback;
                }
                uchar recv[64] = { 0 };
                if(!SramGetHeaderState(p->pmac, recv, sizeof(recv)))
                {
                    if(p->sys_info.cam == 2)
                    {
                        phead_test->dumu_sensor[0] = (recv[39] & 0x02) ? 0 : 1;
                        phead_test->dumu_sensor[0] |= (recv[39] & 0x01) ? 0 : 2;
                        phead_test->dumu_sensor[0] |= (recv[38] & 0x10) ? 0 : 4;
                        phead_test->dumu_sensor[0] |= (recv[38] & 0x08) ? 0 : 8;
                        phead_test->dumu_sensor[1] = (recv[38] & 0x80) ? 0 : 1;
                        phead_test->dumu_sensor[1] |= (recv[38] & 0x40) ? 0 : 2;
                        phead_test->dumu_sensor[1] |= (recv[38] & 0x04) ? 0 : 4;
                        phead_test->dumu_sensor[1] |= (recv[38] & 0x02) ? 0 : 8;
                        phead_test->sink_sensor[0] = (recv[38] & 0x20) ? 0 : 1;
                        phead_test->sink_sensor[1] = (recv[38] & 0x01) ? 0 : 1;
                    }
                    else
                    {
                        phead_test->dumu_sensor[0] = recv[38] & 0x20 ? 0 : 1;
                        phead_test->dumu_sensor[0] |= recv[38] & 0x10 ? 0 : 2;
                        phead_test->dumu_sensor[1] = recv[38] & 0x08 ? 0 : 1;
                        phead_test->dumu_sensor[1] |= recv[38] & 0x04 ? 0 : 2;
                    }
                }
            }
        }
        else
        {
            ret = MacGetHeader(p->pmac, phead_test);
            if(!ret)
            {
                phead_test->yarn_state[0] = ~phead_test->yarn_state[0];
                phead_test->yarn_state[1] = ~phead_test->yarn_state[1];
                phead_test->cam_state[0][0] = phead_test->cam_state[0][0] ^ 0x2D;
                phead_test->cam_state[1][0] = phead_test->cam_state[1][0] ^ 0x2D;
                phead_test->cam_state[0][1] = phead_test->cam_state[0][1] ^ 0x2D;
                phead_test->cam_state[1][1] = phead_test->cam_state[1][1] ^ 0x2D;
                phead_test->select_state[0][0] = ~phead_test->select_state[0][0];
                phead_test->select_state[0][1] = ~phead_test->select_state[0][1];
                phead_test->select_state[1][0] = ~phead_test->select_state[1][0];
                phead_test->select_state[1][1] = ~phead_test->select_state[1][1];
                phead_test->select_state[0][2] =~phead_test->select_state[0][2];
                phead_test->select_state[0][3] = ~phead_test->select_state[0][3];
                phead_test->select_state[1][2] =~phead_test->select_state[1][2];
                phead_test->select_state[1][3] = ~phead_test->select_state[1][3];

                memcpy(p->dumu_sensor, phead_test->dumu_sensor, 8);
                memcpy(p->sink_sensor, phead_test->sink_sensor, 2);
            }
        }
    }
    return ret;
}
/*
 * get source header state
 */
void GetHeaderSource(MH_KNIT *p, HEAD_TEST *phead_test)
{
    memcpy(phead_test, &p->head_test_backup, sizeof(HEAD_TEST));
}
/*
 * get state of header for running
 */
int GetHeaderState(MH_KNIT *p, HEAD_TEST *phead_test)
{
    int ret = 0;
    uchar recv[64] = {0};
    ret = SramGetHeaderState(p->pmac, recv, sizeof(recv));
    if(!ret)
    {
        phead_test->yarn_state[0] = ~recv[2];
        phead_test->yarn_state[1] = ~recv[3];
        if(p->sys_info.cam == 2)
        {
            short dumu[10] = { 0 };
            memcpy(dumu, &recv[4], sizeof(dumu));
            phead_test->dumu_state[0][0] = dumu[0];
            phead_test->dumu_state[0][1] = dumu[1];
            phead_test->dumu_state[1][0] = dumu[2];
            phead_test->dumu_state[1][1] = dumu[3];
            phead_test->sink_state[0][0] = p->front_sink_reset_value - dumu[4];
            phead_test->dumu_state[0][2] = dumu[5];
            phead_test->dumu_state[0][3] = dumu[6];
            phead_test->dumu_state[1][2] = dumu[7];
            phead_test->dumu_state[1][3] = dumu[8];
            phead_test->sink_state[1][0] = p->back_sink_reset_value - dumu[9];
            phead_test->select_state[0][0] = ~recv[28];
            phead_test->select_state[0][1] = ~recv[29];
            phead_test->select_state[0][2] = ~recv[30];
            phead_test->select_state[0][3] = ~recv[31];
            phead_test->select_state[1][0] = ~recv[32];
            phead_test->select_state[1][1] = ~recv[33];
            phead_test->select_state[1][2] = ~recv[34];
            phead_test->select_state[1][3] = ~recv[35];
        }
        else
        {
            short dumu[6] = { 0 };
            memcpy(dumu, &recv[4], sizeof(dumu));
            phead_test->dumu_state[0][0] = dumu[0];
            phead_test->dumu_state[0][1] = dumu[1];
            phead_test->dumu_state[1][0] = dumu[2];
            phead_test->dumu_state[1][1] = dumu[3];
            phead_test->dumu_state[0][2] = 0;
            phead_test->dumu_state[0][3] = 0;
            phead_test->dumu_state[1][2] = 0;
            phead_test->dumu_state[1][3] = 0;
            phead_test->select_state[0][0] = ~recv[28];
            phead_test->select_state[0][1] = ~recv[29];
            phead_test->select_state[1][0] = ~recv[30];
            phead_test->select_state[1][1] = ~recv[31];
        }
        if(p->sys_info.cam == 2)
        {
            //cam
            short s = *((short*)&recv[24]);
            short t = *((short*)&recv[26]);
            s ^= 0x6262;
            t ^= 0x0044;
            s = ~s;
            t = ~t;
            phead_test->cam_state[0][0]  = (s & 0x0040) ? 0x02 : 0;
            phead_test->cam_state[0][0] |= (s & 0x0080) ? 0x01 : 0;
            phead_test->cam_state[0][0] |= (t & 0x0001) ? 0x04 : 0;
            phead_test->cam_state[0][0] |= (t & 0x0002) ? 0x08 : 0;
            phead_test->cam_state[0][0] |= (t & 0x0004) ? 0x10 : 0;
            phead_test->cam_state[0][0] |= (t & 0x0008) ? 0x20 : 0;

            phead_test->cam_state[0][1]  = (s & 0x4000) ? 0x02 : 0;
            phead_test->cam_state[0][1] |= (s & 0x8000) ? 0x01 : 0;
            phead_test->cam_state[0][1] |= (t & 0x0010) ? 0x04 : 0;
            phead_test->cam_state[0][1] |= (t & 0x0020) ? 0x08 : 0;
            phead_test->cam_state[0][1] |= (t & 0x0040) ? 0x10 : 0;
            phead_test->cam_state[0][1] |= (t & 0x0080) ? 0x20 : 0;

            phead_test->cam_state[1][0]  = (s & 0x0100) ? 0x08 : 0;
            phead_test->cam_state[1][0] |= (s & 0x0200) ? 0x10 : 0;
            phead_test->cam_state[1][0] |= (s & 0x0400) ? 0x20 : 0;
            phead_test->cam_state[1][0] |= (s & 0x0800) ? 0x01 : 0;
            phead_test->cam_state[1][0] |= (s & 0x1000) ? 0x04 : 0;
            phead_test->cam_state[1][0] |= (s & 0x2000) ? 0x02 : 0;

            phead_test->cam_state[1][1]  = (s & 0x0001) ? 0x08 : 0;
            phead_test->cam_state[1][1] |= (s & 0x0002) ? 0x10 : 0;
            phead_test->cam_state[1][1] |= (s & 0x0004) ? 0x20 : 0;
            phead_test->cam_state[1][1] |= (s & 0x0008) ? 0x01 : 0;
            phead_test->cam_state[1][1] |= (s & 0x0010) ? 0x04 : 0;
            phead_test->cam_state[1][1] |= (s & 0x0020) ? 0x02 : 0;
            //
            phead_test->dumu_sensor[0] = (recv[39] & 0x02) ? 0 : 1;
            phead_test->dumu_sensor[0] |= (recv[39] & 0x01) ? 0 : 2;
            phead_test->dumu_sensor[0] |= (recv[38] & 0x10) ? 0 : 4;
            phead_test->dumu_sensor[0] |= (recv[38] & 0x08) ? 0 : 8;
            phead_test->dumu_sensor[1] = (recv[38] & 0x80) ? 0 : 1;
            phead_test->dumu_sensor[1] |= (recv[38] & 0x40) ? 0 : 2;
            phead_test->dumu_sensor[1] |= (recv[38] & 0x04) ? 0 : 4;
            phead_test->dumu_sensor[1] |= (recv[38] & 0x02) ? 0 : 8;
            phead_test->sink_sensor[0] = (recv[38] & 0x20) ? 0 : 1;
            phead_test->sink_sensor[1] = (recv[38] & 0x01) ? 0 : 1;
        }
        else
        {
            phead_test->cam_state[0][0] = recv[24] ^ 0x10;
            phead_test->cam_state[1][0] = recv[25] ^ 0x10;
            phead_test->dumu_sensor[0] = recv[38] & 0x20 ? 0 : 1;
            phead_test->dumu_sensor[0] |= recv[38] & 0x10 ? 0 : 2;
            phead_test->dumu_sensor[1] = recv[38] & 0x08 ? 0 : 1;
            phead_test->dumu_sensor[1] |= recv[38] & 0x04 ? 0 : 2;
        }
    }
    return ret;
}
/*
 * initial state of machine header
 */
int InitHeaderState(MH_KNIT *p)
{
    HEAD_TEST head_test = {0};
    int ret = 0;
    if(p->communication == COMM_SRAM)
    {
        ret = GetHeaderState(p, &head_test);
    }
    else
    {
        ret = GetHeadTestState(p, &head_test);
    }
    if(!ret)
    {
        int i = 0;
        p->yarn_state[0] = head_test.yarn_state[0];
        p->yarn_state[1] = head_test.yarn_state[1];
        p->cam_state[0][0] = head_test.cam_state[0][0];
        p->cam_state[1][0] = head_test.cam_state[1][0];
        p->cam_state[0][1] = head_test.cam_state[0][1];
        p->cam_state[1][1] = head_test.cam_state[1][1];
        for(i = 0; i < 8; ++i)
        {
            p->select_state[0][0][i] = (head_test.select_state[0][0] >> i) & 1;
            p->select_state[0][1][i] = (head_test.select_state[0][1] >> i) & 1;
            p->select_state[0][2][i] = (head_test.select_state[0][2] >> i) & 1;
            p->select_state[0][3][i] = (head_test.select_state[0][3] >> i) & 1;
            p->select_state[1][0][i] = (head_test.select_state[1][0] >> i) & 1;
            p->select_state[1][1][i] = (head_test.select_state[1][1] >> i) & 1;
            p->select_state[1][2][i] = (head_test.select_state[1][2] >> i) & 1;
            p->select_state[1][3][i] = (head_test.select_state[1][3] >> i) & 1;
        }
        p->dumu_state[0][0] = head_test.dumu_state[0][0];
        p->dumu_state[0][1] = head_test.dumu_state[0][1];
        p->dumu_state[0][2] = head_test.dumu_state[0][2];
        p->dumu_state[0][3] = head_test.dumu_state[0][3];
        p->dumu_state[1][0] = head_test.dumu_state[1][0];
        p->dumu_state[1][1] = head_test.dumu_state[1][1];
        p->dumu_state[1][2] = head_test.dumu_state[1][2];
        p->dumu_state[1][3] = head_test.dumu_state[1][3];
        p->dumu_sensor[0] = head_test.dumu_sensor[0];
        p->dumu_sensor[1] = head_test.dumu_sensor[1];
        p->dumu_sensor[2] = head_test.dumu_sensor[2];
        p->dumu_sensor[3] = head_test.dumu_sensor[3];
        p->dumu_sensor[4] = head_test.dumu_sensor[4];
        p->dumu_sensor[5] = head_test.dumu_sensor[5];
        p->dumu_sensor[6] = head_test.dumu_sensor[6];
        p->dumu_sensor[7] = head_test.dumu_sensor[7];

        p->sink_state[0][0] = head_test.sink_state[0][0];
        p->sink_state[1][0] = head_test.sink_state[1][0];
        p->sink_sensor[0] = head_test.sink_sensor[0];
        p->sink_sensor[1] = head_test.sink_sensor[1];

        memcpy(&p->head_test_backup, &head_test, sizeof(HEAD_TEST));
    }
    return ret;
}
/*
 * get signal light state
 * return: list below
 */
#define SIGNAL_LIGHT_STATE_OFF         0
#define SIGNAL_LIGHT_STATE_RED         1
#define SIGNAL_LIGHT_STATE_GREEN       2
#define SIGNAL_LIGHT_STATE_YELLOW      4
uchar GetSignalLightState(MH_KNIT *p)
{
    return p->signal_light_state;
}
/*
 *
 */
int ToggleSignalLight(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        switch (p->signal_light_state)
        {
        case SIGNAL_LIGHT_STATE_OFF:
            p->signal_light_state = SIGNAL_LIGHT_STATE_RED;
            if(p->communication == COMM_SRAM)
            {
                ret = SramSwitchRedLight(p->pmac, TRUE);
            }
            else
            {
                ret = SwitchRedLight(p->pmac, TRUE);
            }
            break;
        case SIGNAL_LIGHT_STATE_RED:
            p->signal_light_state = SIGNAL_LIGHT_STATE_GREEN;
            if(p->communication == COMM_SRAM)
            {
                ret = SramSwitchRedLight(p->pmac, FALSE);
                if(!ret)
                {
                    ret = SramSwitchGreenLight(p->pmac, TRUE);
                }
            }
            else
            {
                ret = SwitchGreenLight(p->pmac, TRUE);
            }
            break;
        case SIGNAL_LIGHT_STATE_GREEN:
            p->signal_light_state = SIGNAL_LIGHT_STATE_YELLOW;
            if(p->communication == COMM_SRAM)
            {
                ret = SramSwitchGreenLight(p->pmac, FALSE);
                if(!ret)
                {
                    ret = SramSwitchYellowLight(p->pmac, TRUE);
                }
            }
            else
            {
                ret = SwitchYellowLight(p->pmac, TRUE);
            }
            break;
        case SIGNAL_LIGHT_STATE_YELLOW:
            p->signal_light_state = SIGNAL_LIGHT_STATE_OFF;
            if(p->communication == COMM_SRAM)
            {
                ret = SramSwitchYellowLight(p->pmac, FALSE);
            }
            else
            {
                ret = SwitchYellowLight(p->pmac, FALSE);

            }
            break;
        }
    }
    return ret;
}
/*
 * toggle red light
 * param state: 0-off, 1-on
 */
int ToggleRedLight(MH_KNIT *p, int state)
{
    int ret = 0;
    if(p->communication == COMM_SRAM)
    {
        ret = SramSwitchRedLight(p->pmac, state);
    }
    else
    {
        ret = SwitchRedLight(p->pmac, state);
    }
    return ret;
}
/*
 * toggle yellow light
 * param state: 0-off, 1-on
 */
int ToggleYellowLight(MH_KNIT *p, int state)
{
    int ret = 0;
    if(p->communication == COMM_SRAM)
    {
        ret = SramSwitchYellowLight(p->pmac, state);
    }
    else
    {
        ret = SwitchYellowLight(p->pmac, state);
    }
    return ret;
}
/*
 * toggle green light
 * param state: 0-off, 1-on
 */
int ToggleGreenLight(MH_KNIT *p, int state)
{
    int ret = 0;
    if(p->communication == COMM_SRAM)
    {
        ret = SramSwitchGreenLight(p->pmac, state);
    }
    else
    {
        ret = SwitchGreenLight(p->pmac, state);
    }
    return ret;
}
/*
 * run the rab
 * param speed: 0-stop, <0-reverse, >0-normal
 */
int RunRab(MH_KNIT *p, uchar speed, int pulse)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            if(speed < 0)
            {
                speed = -GetHighRollerFrequency(p, -speed);
            }
            else
            {
                speed = GetHighRollerFrequency(p, speed);
            }
            ret = SramRunRab(p->pmac, speed);
        }
        else
        {
            ret = MacRunRab(p->pmac, speed, pulse);
        }
    }
    return ret;
}
/*
 * run the roller
 * param speed: 0-stop, <0-reverse, >0-normal
 */
int RunRoller(MH_KNIT *p, int speed)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramRunRoller(p->pmac, speed);
        }
        else
        {
            ret = MacRunRoller(p->pmac, speed);
        }
    }
    return ret;
}
/*
 * get enable of machine driver
 */
uchar GetMacDriverEnable(MH_KNIT *p)
{
    return p->mac_driver_enable;
}
/*
 * get enable of machine driver
 */
uchar GetMacDir(MH_KNIT *p)
{
    return p->mac_dir;
}/*
 * get enable of machine driver
 */
void SetMacDir(MH_KNIT *p, uchar n)
{
    p->mac_dir = n;
}
/*
 * toggle machine driver
 */
int ToggleMacDriverEnable(MH_KNIT *p)
{
    p->mac_driver_enable = !p->mac_driver_enable;
    return -1;
}
/*
 * get state of left yarn feeder
 * return: 0-stop, 1-run
 */
uchar GetLeftYarnFeederState(MH_KNIT *p)
{
    return p->left_yarn_feeder_state;
}
/*
 * toggle state of left yarn feeder
 */
int ToggleLeftYarnFeeder(MH_KNIT *p)
{
    int ret = 0;
    p->left_yarn_feeder_state = !p->left_yarn_feeder_state;
    if(p->communication == COMM_SRAM)
    {
        uchar speed = 0;
        if(p->left_yarn_feeder_state)
        {
            speed = 50;
        }
        ret = SramRunFeeder(p->pmac, FALSE, speed);
    }
    else
    {
       if(p->left_yarn_feeder_state)
        {
            ret = SwitchYarnFeeder(p->pmac, 1, 50);
        }
        else
        {
             ret = SwitchYarnFeeder(p->pmac, 1, 0);
        }
    }
    return ret;
}
/*
 * get state of right yarn feeder
 * return: 0-stop, 1-run
 */
uchar GetRightYarnFeederState(MH_KNIT *p)
{
    return p->right_yarn_feeder_state;
}
/*
 * toggle state of right yarn feeder
 */
int ToggleRightYarnFeeder(MH_KNIT *p)
{
    int ret = 0;
    p->right_yarn_feeder_state = !p->right_yarn_feeder_state;
    if(p->communication == COMM_SRAM)
    {
        uchar speed = 0;
        if(p->right_yarn_feeder_state)
        {
            speed = 50;
        }
        ret = SramRunFeeder(p->pmac, TRUE, speed);
    }
    else
    {
           if(p->right_yarn_feeder_state)
        {
            ret = SwitchYarnFeeder(p->pmac, 2, 50);
        }
        else
        {
            ret = SwitchYarnFeeder(p->pmac, 2, 0);
        }
    }
    return ret;
}
/*
 * run machine motor
 */
int RunMotor(MH_KNIT *p, int speed)
{
    int ret = 0;
    if(p->machine_ok)
    {
        int drt = 0;
        if(speed < 0)
        {
            drt = 1;
            speed = -speed;
        }
        else if(speed > 0)
        {
            drt = 2;
        }
        if(p->communication == COMM_SRAM)
        {
            ret = SramRunMotor(p->pmac, drt, speed);
        }
        else
        {
            ret = MacRunMotor(p->pmac, drt, speed);
        }
    }
    return ret;
}
/*
 * run rock
 */
int RunRock(MH_KNIT *p, short silk)
{
    int ret = 0;
    silk *= GetRockPulsePerSilk(p);
    uchar mode = GetRockType(p);
    if(mode == ROCK_FRONT_BACK)
    {
        silk /= 2;
    }
    else if(mode == ROCK_BACK)
    {
        silk = -silk;
    }
    printf("Run Rock des = %d, src = %d\n", silk, p->rock_pos);
    if(silk != p->rock_pos)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramRunRock(p->pmac, silk - p->rock_pos, p->rock_speed, 0);
        }
        else
        {
            ret = MacRunRock(p->pmac, silk);
        }
        if(!ret)
        {
            p->rock_pos = silk;
        }
    }
    return ret;
}
/*
 * reset rock
 */
int ResetRock(MH_KNIT *p, short silk)
{
    int ret = 0;
    if(p->communication == COMM_SRAM)
    {
        silk *= GetRockPulsePerSilk(p);
        uchar mode = GetRockType(p);
        if(mode == ROCK_FRONT_BACK)
        {
            silk /= 2;
        }
        ret = SramRunRock(p->pmac, 0, p->rock_speed, silk);
    }
    else
    {
        ret = MacResetRock(p->pmac, silk);
    }
    if(!ret)
    {
        p->rock_pos = 0;
    }
    return ret;
}
/*
 * reset rock
 */
int RunRockNeedle(MH_KNIT *p, int needle)
{
    int ret = 0;
    ret = MacRunRock(p->pmac, needle);
    return ret;
}
/*
 * get rock position from mac
 */
int GetRockPosNeedle(MH_KNIT *p)
{
    return p->rock_pos_src;
}
/*
 * set rock position ,position data from mac
 */
void SetRockPosNeedle(MH_KNIT *p, int needle)
{
    p->rock_pos_src = needle;
}
/*
 * get run teset needle
 */
int GetRockTestNeedle(MH_KNIT *p)
{
    return p->rock_needle;
}
/*
 * set rock position ,position data from mac
 */
void SetRockTestNeedle(MH_KNIT *p, ushort needle)
{
    p->rock_needle = needle;
}
/*
 * run rock for needle
 */
int RockNeedle(MH_KNIT *p, short needle)
{
    int ret = 0;
    float fmm = 0;
    if(p->inch_type > 0.8)
    {
        fmm = CM_PER_INCH * 1000.0 / p->inch_type; //0.01mm/needle
    }
    ret = RunRock(p, fmm * needle);
    return ret;
}



/*
 * set state of system include TEST, RESET, KNIT, HALT...
 */
void SetSystemState(MH_KNIT *p, int state)
{
    p->sys_state = state;
}
/*
 * get state of system include TEST, RESET, KNIT, HALT
 */
int GetSystemState(MH_KNIT *p)
{
    return p->sys_state;
}
/*
 * get signal of rack
 */
typedef struct{
    uchar front_shake;
    uchar back_shake;
    uchar mac_warn;
    uchar rock_1warn;
    uchar rock_2warn;
    uchar ac_110v;
    uchar left_yarn_feeder_break;
    uchar right_yarn_feeder_break;

    uchar antenna_break;
    uchar knot;
    uchar handle_stop;
    uchar handle_slow;
    uchar handle_quick;
    uchar right_back_safe_door;
    uchar left_forbid;
    uchar right_forbid;

    uchar left_break;
    uchar right_break;
    uchar front_safe_door;
    uchar back_safe_door;
    uchar rock_zero;
    uchar rock_forbid;
    uchar rsvd1;
    uchar rsvd2;

    uchar rab_warn;
    uchar needle_zero;
    uchar roll_warn;
    uchar ups;
    uchar rock_2zero;
    uchar rsvd3;
    uchar scram;
    uchar yarn_store_dev;

    uchar drop;
    uchar board_safe_door;
    uchar drop_bad;
    uchar rab_open;
    uchar left_elastic;
    uchar right_elastic;
    uchar dc_12v;
    uchar dc_24v;

    uchar rsvd4;
    uchar rsvd5;
    uchar rsvd6;
    uchar rsvd7;
    uchar rsvd8;
    uchar rsvd9;
    uchar front_probe;
    uchar back_probe;
}RACK_SIGNAL;
 int GetRackSignal(MH_KNIT *p, RACK_SIGNAL *prack_signal, short *needle)
{
    int ret = 0;
    if(p->machine_ok)
    {
        uchar signal[6] = { 0 };
        if(p->communication == COMM_SRAM)
        {
            int pulse = 0;
            ret = SramGetRackSignal(p->pmac, signal, sizeof(signal), &pulse);
            signal[0] = ~signal[0];
            signal[1] = ~signal[1];
            signal[2] = ~signal[2];
            signal[3] = ~signal[3];
            signal[4] = ~signal[4];
            signal[5] = ~signal[5];
            SRAM_RACK *rack = (SRAM_RACK*)signal;
            prack_signal->left_yarn_feeder_break = rack->left_feeder;
            prack_signal->right_yarn_feeder_break = rack->right_feeder;
            prack_signal->antenna_break = rack->antenna;
            prack_signal->knot = rack->knot;
            prack_signal->handle_stop = rack->stop;
            prack_signal->handle_slow = rack->slow;
            prack_signal->handle_quick = rack->quick;
            prack_signal->drop = rack->drop;

            prack_signal->left_forbid = rack->left_forbid;
            prack_signal->right_forbid = rack->right_forbid;
            prack_signal->left_break = rack->left_break;
            prack_signal->right_break = rack->right_break;
            prack_signal->front_safe_door = rack->front_safe_door;
            prack_signal->back_safe_door = rack->back_safe_door;
            prack_signal->rock_zero = rack->rock_start;
            prack_signal->rock_forbid = rack->rock_end;

            prack_signal->rab_warn = rack->rab;
            prack_signal->needle_zero = rack->need_zero;
            prack_signal->scram = rack->scram;
            prack_signal->yarn_store_dev = rack->store;
            prack_signal->ups = rack->ups;

            prack_signal->drop_bad = rack->drop_ray;
            //rack->code_a;
            //rack->code_b;
            prack_signal->left_elastic = rack->left_rubber;
            prack_signal->right_elastic = rack->right_rubber;
            //rack->kaihe;
            prack_signal->front_shake = rack->front_shake;
            prack_signal->back_shake = rack->back_shake;

            prack_signal->mac_warn = rack->motor_drive;

            prack_signal->rock_1warn = rack->rock_drive;

            prack_signal->back_probe = rack->back_probe;
            prack_signal->front_probe = rack->front_probe;


            if(p->belt_right_com - p->belt_left_com)
            {
                *needle = (pulse - p->belt_left_com) * p->total_needle / (p->belt_right_com - p->belt_left_com);
            }
        }
        else
        {
            ret = MacGetRackSignal(p->pmac, signal, sizeof(signal), needle);
            if(!ret)
            {
                int i = 0;
                for (i = 0; i < sizeof(RACK_SIGNAL); ++i)
                {
                    ((uchar*) prack_signal)[i] = (signal[i / 8] >> (i & 7)) & 0x01;
                }
            }
        }
    }
    return ret;
}
/*
 *
 */
#pragma pack(1)
struct _BASE_DATA
{
    int mac_type;        //0
    int mac_size;        //1
    int needle_zero;     //2
    int total_pulse;     //3
    int dist_1select;    //4
    int dist_2select;    //5
    int dist_3select;    //6
    int dist_4select;    //7
    int rock_max_speed;  //8
    int rock_max_add;    //9
    int rock_add;        //10
    int main_servo;      //11
    int board_high_pos;  //12
    int board_safe_pos;  //13
    int roll_close_for_board;//14
    int rsvd1[2];           //15-16
    int hook_open_pos;      //17
    int right_gauze_right_com;//18
    int right_gauze_left_com;//19
    int left_gauze_right_com;//20
    int left_gauze_left_com;//21
    int rock_mode;//22
    int select_sect;//23
    int sink1_front_com;//24
    int sink1_back_com;//25
    int rock_com;//26
    int board_enable;//27
    int board_raise_1speed;//28
    int board_raise_2speed;//29
    int board_raise_3speed;//30
    int board_drop_speed_for_safe;//31
    int board_reset_speed;//32
    int board_drop_pos;//33
    int board_drop_speed;//34
    int select_left_com;//35
    int select_right_com;//36
    int rab_speed_for_drop;//37
    int board_drop_delay;//38
    int board_speed_for_open_hook;//39
    int shake_sense;//40
    int sink_mode;//41
    ushort yarn_feeder_mode;
    ushort yarn_feeder_delay;//42
    struct
    {
        uint cam :3;
        uint motor :13;
        uint gauze :8;
        uint mac :8;
    } sys_info;              //43
    uchar motor_mole;
    uchar motor_deno;
    ushort rsvd2;            //44
    int warn_forbid;         //45
    int rab_speed;           //46
    uchar scissor_times;
    uchar knot_line;
    ushort rsvd3;            //47
    ushort board_raise_wait;
    ushort board_version;    //48
    ushort yarn_left_out;
    ushort yarn_right_out;   //49
    int cam_reset_state;     //50
    int rab_close_for_board;      //51
    ushort board_rab_close_speed;
    ushort board_rab_open_speed;//52
    struct
    {
        uint roll_enable :1;         //主拉布
        uint rab_enable :1;          //高罗拉
        uint right_jt_enable :1;
        uint rsvd :29;
    } rab_option;               //53
    int hook_open_pos_for_rab; //54
    int dist_for_2header;       //55
    ushort sink2_front_com;
    ushort sink2_back_com;      //56
    int back_needle_zero;       //57
    struct
    {
        uint rock : 1;
        uint select_repeat : 1;
        uint sink : 1;
        uint rsvd : 29;
    }zero_drt;                  //58
    int rab_open_com;           //59
    int probe_distance;         //60
    int dist_1dumu;             //61
    int dist_2dumu;             //62
    int dist_3dumu;             //63
    int dist_4dumu;             //64
    short dmbz_yarn_f1;
    short dmbz_yarn_b1;         //65
    short dmbz_yarn_f2;
    short dmbz_yarn_b2;         //66
    short dmbz_yarn_f3;
    short dmbz_yarn_b3;         //67
    short dmbz_yarn_f4;
    short dmbz_yarn_b4;         //68
    int dumu_sprt;               //69
    int rsvd4;                   //70
    int rsvd5;                   //71
    uchar gauze_type;
    uchar driver_type;
    ushort rsvd6;                //72
};
#pragma pack()
typedef struct _BASE_DATA     BASE_DATA;
/*
 * get base data
 */
void GetBaseData(MH_KNIT *p, BASE_DATA *data)
{
    //printf("base_data: %d\n", sizeof(BASE_DATA));
    data->mac_type = p->inch_type;
    if(p->inch_type)
    {
        data->mac_size = p->total_needle / p->inch_type;
        p->rock_star = (CM_PER_INCH * 1000.0 / p->inch_type) / 2;
    }
    data->total_pulse = p->belt_right_com - p->belt_left_com;
    //if(p->total_needle)
    {
        //data->needle_zero = p->needle_zero * data->total_pulse / p->total_needle;
        data->needle_zero = p->belt_left_com;
    }
    if(p->communication == COMM_SRAM)
    {
        if(p->sys_info.cam == 2)
        {
            data->dist_1select = p->select4_to_mac_left_len * p->inch_type / MM_PER_INCH;
            data->dist_2select = p->select3_to_mac_left_len * p->inch_type / MM_PER_INCH;
            data->dist_3select = p->select2_to_mac_left_len * p->inch_type / MM_PER_INCH;
            data->dist_4select = p->select1_to_mac_left_len * p->inch_type / MM_PER_INCH;
        }
        else
        {
            data->dist_1select = p->select2_to_mac_left_len * p->inch_type / MM_PER_INCH;
            data->dist_2select = p->select1_to_mac_left_len * p->inch_type / MM_PER_INCH;
        }
    }
    else
    {
        data->dist_1select = p->select1_to_mac_left_len;
        data->dist_2select = p->select2_to_mac_left_len;
        data->dist_3select = p->select3_to_mac_left_len;
        data->dist_4select = p->select4_to_mac_left_len;
    }
    data->rock_max_speed = 30000;
    data->rock_max_add = 10;
    data->rock_add = 100;
    data->main_servo = GetMotorPulsePerMeter(p);
    if(data->total_pulse)
    {
        data->right_gauze_right_com = p->right_yarn_right_com;
        data->right_gauze_left_com = p->right_yarn_left_com;
        data->left_gauze_right_com = p->left_yarn_right_com;
        data->left_gauze_left_com = p->left_yarn_left_com;
    }
    data->rock_mode = p->rock_type;
    data->select_sect = p->select_sect;
    data->sink1_front_com = p->front_sink_reset_value;
    data->sink1_back_com = p->back_sink_reset_value;
    data->rock_com = GetRockZeroCom(p) * GetRockPulsePerSilk(p);

    if(data->rock_mode == ROCK_FRONT_BACK)
    {
        data->rock_com /= 2;
        p->rock_star /= 2;
    }
    data->board_enable = p->board_enable;

    data->board_high_pos = p->board_param.high_pos;
    data->board_safe_pos = p->board_param.safe_pos;
    data->roll_close_for_board = p->board_param.pos_for_roll_close;
    data->hook_open_pos = p->board_param.hook_open_pos_for_roll - p->board_param.delay_for_hook_open;
    data->board_raise_1speed = p->board_param.speed_for_up_start;
    data->board_raise_2speed = p->board_param.speed_for_up_quick;
    data->board_raise_3speed = p->board_param.speed_for_up_slow;
    data->board_drop_speed_for_safe = p->board_param.speed_for_drop_2safe_pos;
    data->board_reset_speed = p->board_param.reset_speed;
    data->board_drop_pos = p->board_param.pos_for_down_slow;
    data->board_drop_speed = p->board_param.speed_for_down_slow;
    if(p->total_needle)
    {
        data->select_left_com = data->total_pulse / p->total_needle * p->select_left_com / 10;
        data->select_right_com = data->total_pulse / p->total_needle * p->select_right_com / 10;
    }
    data->rab_speed_for_drop = p->board_param.speed_for_roll_when_drop;
    data->board_drop_delay = p->board_param.wait_for_drop;
    data->board_speed_for_open_hook = p->board_param.speed_down_after_hook_open;
    data->shake_sense = 2200 - p->front_shake_sense * 14;
    data->sink_mode = p->sink_enable ? TRUE : FALSE;
    data->yarn_feeder_mode = p->yarn_feeder_mode == 1 ? 1 : 0;
    data->yarn_feeder_delay = p->feeder_start_wait;
    data->sys_info.cam = p->sys_info.cam - 1;
    if(p->sys_info.cam == 2)
    {
        data->sys_info.motor = 0;
    }
    else
    {
        data->sys_info.motor = p->sys_info.motor;
    }
    data->sys_info.gauze = p->sys_info.gauze;
    if(p->sys_info.mac == 0)
    {
        data->sys_info.mac = p->cam_type;
    }
    else
    {
        data->sys_info.mac = p->sys_info.mac;
    }
    data->motor_mole = p->motor_drive.motor_elec_molecular;
    data->motor_deno = p->motor_drive.motor_elec_denominator;
    data->warn_forbid = GetAlarmMask(p);
    data->rab_speed = GetHighRollerFrequency(p, 90);//10 + (200-10)/99.0 * 90;
    data->scissor_times = p->board_param.scissor_times;
    data->knot_line = p->knot_wait_line;
    data->board_raise_wait = p->board_param.pos_for_up_wait;
    data->board_version = 1;//p->board_param.board_type;
    data->yarn_left_out = GetYarnLeftOutStop(p);
    data->yarn_right_out = GetYarnRightOutStop(p);
    data->cam_reset_state = 0x1010;//0x3F3F;
    data->rab_close_for_board = p->board_param.pos_for_rab_close;
    data->board_rab_close_speed = p->board_param.rab_close_speed;
    data->board_rab_open_speed = p->board_param.rab_open_speed;
    data->rab_option.rab_enable = p->knit_state.rab_enable;
    data->rab_option.roll_enable = p->knit_state.roll_enable;
    //data.rab_option.right_jt_disable = p->right_jt_disable;
    data->hook_open_pos_for_rab = p->board_param.hook_open_pos_for_rab;
    //data.dist_for_2header = p->inch_type / MM_PER_INCH * p->dist_for_2header;
    //data.sink2_front_com = p->sink2_front_com;
    //data.sink2_back_com = p->sink2_back_com;
    if(p->total_needle)
    {
        data->back_needle_zero = p->belt_left_com + data->total_pulse / (p->total_needle * 2);
    }
    data->zero_drt.rock = p->knit_state.rock_zero_drt;
    data->zero_drt.select_repeat = p->select_repeat_enable;
    data->zero_drt.sink = p->knit_state.sink_drt;
    data->rab_open_com = p->roll_open_com;
    data->probe_distance = p->probe_distance * p->inch_type / MM_PER_INCH;
    if(p->sys_info.cam == 2)
    {
        data->dist_1dumu = p->dumu4_to_mac_left_len * p->inch_type / MM_PER_INCH;
        data->dist_2dumu = p->dumu3_to_mac_left_len * p->inch_type / MM_PER_INCH;
        data->dist_3dumu = p->dumu2_to_mac_left_len * p->inch_type / MM_PER_INCH;
        data->dist_4dumu = p->dumu1_to_mac_left_len * p->inch_type / MM_PER_INCH;
    }
    else
    {
        data->dist_1dumu = p->dumu2_to_mac_left_len * p->inch_type / MM_PER_INCH;
        data->dist_2dumu = p->dumu1_to_mac_left_len * p->inch_type / MM_PER_INCH;
    }
    data->dmbz_yarn_b1 = p->dmbz_yarn_com[1][0];
    data->dmbz_yarn_b2 = p->dmbz_yarn_com[1][1];
    data->dmbz_yarn_b3 = p->dmbz_yarn_com[1][2];
    data->dmbz_yarn_b4 = p->dmbz_yarn_com[1][3];
    data->dmbz_yarn_f1 = p->dmbz_yarn_com[0][0];
    data->dmbz_yarn_f2 = p->dmbz_yarn_com[0][1];
    data->dmbz_yarn_f3 = p->dmbz_yarn_com[0][2];
    data->dmbz_yarn_f4 = p->dmbz_yarn_com[0][3];

    data->dumu_sprt = p->mac.dumu_sprt;
    data->gauze_type = p->gauze_type;
    data->driver_type = p->mac.driver_type;
}
/*
 *
 */
#pragma pack(1)
struct _SYS_DATA
{
    uchar select[8];
    ushort gauze_time;//8-9
    ushort cam_time;//10-11
    ushort select_time;//12-13
    uchar dumu_reset_speed;//14
    uchar dumu_work_speed;//15
    uchar cam_work_speed;//16
    uchar sink_work_speed;//17
    uchar rsvd[14];
    uchar board_com;
    ushort board_pos_for_up_quick;
    ushort board_pos_for_up_slow;
};
#pragma pack()
typedef struct _SYS_DATA   SYS_DATA;
/*
 *
 */
void GetSysData(MH_KNIT *p, SYS_DATA *data)
{
    //printf("sys_data: %d\n", sizeof(SYS_DATA));
    memcpy(data->select, p->select, __min(sizeof(p->select), p->select_sect));
    data->gauze_time = p->gauze_high_voltage;
    data->cam_time = p->cam_high_voltage;
    data->select_time = p->select_high_voltage;
    data->dumu_reset_speed = p->dumu_reset_speed;
    data->dumu_work_speed = p->dumu_work_speed;
    //data->cam_work_speed = p->sink_work_speed;
    data->sink_work_speed = p->sink_work_speed;
    data->board_com = p->board_param.balance;
    data->board_pos_for_up_quick = p->board_param.pos_for_up_quick;
    data->board_pos_for_up_slow = p->board_param.pos_for_up_slow;
}
/*
 * turn on/off machine which decides to communicate or not
 */
void TurnOnMachine(MH_KNIT *p, int bOn)
{
    p->machine_ok = bOn;
}
/**************************************************************************
 * about parts param of machine from every company below
 **************************************************************************/
/*
 * get select1's distance to left edge of machine
 */
short GetSelect1DistanceToMacLeft(MH_KNIT *p)
{
    return p->select1_to_mac_left_len;
}
/*
 * set select1's distance to left edge of machine
 */
void SetSelect1DistanceToMacLeft(MH_KNIT *p, short n)
{
    p->select1_to_mac_left_len = n;
}
/*
 * get select2's distance to left edge of machine
 */
short GetSelect2DistanceToMacLeft(MH_KNIT *p)
{
    return p->select2_to_mac_left_len;
}
/*
 * set select2's distance to left edge of machine
 */
void SetSelect2DistanceToMacLeft(MH_KNIT *p, short n)
{
    p->select2_to_mac_left_len = n;
}
/*
 * get select3's distance to left edge of machine
 */
short GetSelect3DistanceToMacLeft(MH_KNIT *p)
{
    return p->select3_to_mac_left_len;
}
/*
 * set select3's distance to left edge of machine
 */
void SetSelect3DistanceToMacLeft(MH_KNIT *p, short n)
{
    p->select3_to_mac_left_len = n;
}
/*
 * get select4's distance to left edge of machine
 */
short GetSelect4DistanceToMacLeft(MH_KNIT *p)
{
    return p->select4_to_mac_left_len;
}
/*
 * set select4's distance to left edge of machine
 */
void SetSelect4DistanceToMacLeft(MH_KNIT *p, short n)
{
    p->select4_to_mac_left_len = n;
}
//////////////////////////////////////////////////
/*
 *
 */
short GetYarnType(MH_KNIT *p)
{
    return p->yarn_type;
}
/*
 *
 */
void SetYarnType(MH_KNIT *p, short n)
{
    p->yarn_type = n;
}
/*
 *
 */
short GetDumu1ToMacLeftLen(MH_KNIT *p)
{
    return p->dumu1_to_mac_left_len;
}
/*
 *
 */
void SetDumu1ToMacLeftLen(MH_KNIT *p, short n)
{
    p->dumu1_to_mac_left_len = n;
}
/*
 *
 */
short GetDumu3ToMacLeftLen(MH_KNIT *p)
{
    return p->dumu3_to_mac_left_len;
}
/*
 *
 */
void SetDumu3ToMacLeftLen(MH_KNIT *p, short n)
{
    p->dumu3_to_mac_left_len = n;
}

/*
 *
 */
short GetSink1ToMacLeftLen(MH_KNIT *p)
{
    return p->sink1_to_mac_left_len;
}
/*
 *
 */
void SetSink1ToMacLeftLen(MH_KNIT *p, short n)
{
    p->sink1_to_mac_left_len = n;
}
/*
 *
 */
short GetSink2ToMacLeftLen(MH_KNIT *p)
{
    return p->sink2_to_mac_left_len;
}
/*
 *
 */
void SetSink2ToMacLeftLen(MH_KNIT *p, short n)
{
    p->sink2_to_mac_left_len = n;
}
/*
 *
 */
short GetFuelGab(MH_KNIT *p)
{
    return p->fuel_gab;
}
/*
 *
 */
void SetFuelGab(MH_KNIT *p, short n)
{
    p->fuel_gab = n;
}
/*
 *
 */
short GetSelect5ToMacRightLen(MH_KNIT *p)
{
    return p->select5_to_mac_right_len;
}
/*
 *
 */
void SetSelect5ToMacRightLen(MH_KNIT *p, short n)
{
    p->select5_to_mac_right_len = n;
}
/*
 *
 */
short GetMacWidth(MH_KNIT *p)
{
    return p->mac_width;
}
/*
 *
 */
void SetMacWidth(MH_KNIT *p, short n)
{
    p->mac_width = n;
}
/*
 *
 */
short GetRockMode(MH_KNIT *p)
{
    return p->rock_mode;
}
/*
 *
 */
void SetRockMode(MH_KNIT *p, short n)
{
    p->rock_mode = n;
}
/*
 *
 */
short GetRockZeroDir(MH_KNIT *p)
{
    return p->rock_zero_dir;
}
/*
 *
 */
void SetRockZeroDir(MH_KNIT *p, short n)
{
    p->rock_zero_dir = n;
}
/*
 *
 */
short GetRedLightTime(MH_KNIT *p)
{
    return p->red_light_time;
}
/*
 *
 */
void SetRedLightTime(MH_KNIT *p, short n)
{
    p->red_light_time = n;
}
/*
 *
 */
short GetSeverType(MH_KNIT *p)
{
    return p->sever_type;
}
/*
 *
 */
void SetSeverType(MH_KNIT *p, short n)
{
    p->sever_type = n;
}
/*
 *
 */
short GetSinkDir(MH_KNIT *p)
{
    return p->sink_dir;
}
/*
 *
 */
void SetSinkDir(MH_KNIT *p, short n)
{
    p->sink_dir = n;
}
/*
 *
 */
short GetDumu2ToMacLeft(MH_KNIT *p)
{
    return p->dumu2_to_mac_left_len;
}
/*
 *
 */
void SetDumu2ToMacLeft(MH_KNIT *p, short n)
{
    p->dumu2_to_mac_left_len = n;
}
/*
 *
 */
short GetDumu4ToMacLeft(MH_KNIT *p)
{
    return p->dumu4_to_mac_left_len;
}
/*
 *
 */
void SetDumu4ToMacLeft(MH_KNIT *p, short n)
{
    p->dumu4_to_mac_left_len = n;
}
/*
 *
 */
short GetFuelTime(MH_KNIT *p)
{
    return p->fuel_time;
}
/*
 *
 */
void SetFuelTime(MH_KNIT *p, short n)
{
    p->fuel_time = n;
}
/*
 *
 */
short GetSelect6ToMacRightLen(MH_KNIT *p)
{
    return p->select6_to_mac_right_len;
}
/*
 *
 */
void SetSelect6ToMacRightLen(MH_KNIT *p, short n)
{
    p->select6_to_mac_right_len = n;
}

/*
 *
 */
short GetProbeDistance(MH_KNIT *p)
{
    return p->probe_distance;
}
/*
 *
 */
void SetProbeDistance(MH_KNIT *p, short n)
{
    p->probe_distance = n;
}
/*
 *
 */
uchar GetProbeShield(MH_KNIT *p)
{
    return p->probe_shield;
}
/*
 *
 */
void SetProbeShield(MH_KNIT *p, uchar n)
{
    p->probe_shield = n;
}
/*
 *
 */
uchar GetDumuAddPercent(MH_KNIT *p)
{
    return p->dumu_add_percent;
}
/*
 *
 */
void SetDumuAddPercent(MH_KNIT *p, uchar n)
{
    p->dumu_add_percent = n;
}
/*
 *
 */
int GetTranDumuEnable(MH_KNIT *p)
{
    return p->knit_state.tran_dumu_enable;
}
/*
 *
 */
void SetTranDumuEnable(MH_KNIT *p, int enable)
{
    p->knit_state.tran_dumu_enable = enable;
}
/*
 *
 */
void GetRockDriveData(MH_KNIT *p, PROCK_DRIVE prock)
{
    memcpy(prock, &p->rock_drive, sizeof(ROCK_DRIVE));
}
/*
 *
 */
void SetRockDriveData(MH_KNIT *p, PROCK_DRIVE prock)
{
    memcpy(&p->rock_drive, prock, sizeof(ROCK_DRIVE));
#if 0
    printf("%d, %d, %d, %d, %d, %d\n", p->rock_drive.rock_coder,
            p->rock_drive.rock_elec_molecular,
            p->rock_drive.rock_elec_denominator,
            p->rock_drive.rock_drive_wheel,
            p->rock_drive.rock_pass_wheel,
            p->rock_drive.rock_pitch);
#endif
}
/*
 *
 */
void GetMotorDriveData(MH_KNIT *p, PMOTOR_DRIVE pmotor)
{
    memcpy(pmotor, &p->motor_drive, sizeof(MOTOR_DRIVE));
}
/*
 *
 */
void SetMotorDriveData(MH_KNIT *p, PMOTOR_DRIVE pmotor)
{
    memcpy(&p->motor_drive, pmotor, sizeof(MOTOR_DRIVE));
}
/*
 *
 */
float GetRockPulsePerSilk(MH_KNIT *p)
{
    ROCK_DRIVE *prock = &p->rock_drive;
    int Denominator = prock->rock_drive_wheel * prock->rock_pitch
            * prock->rock_elec_molecular;
    int molecular = prock->rock_coder * prock->rock_pass_wheel * 4
            * prock->rock_elec_denominator;
    float f = 0.0;
    if(Denominator)
    {
        f = molecular / (Denominator * 100.0);
    }
    //printf("%d, %d, %f\n", molecular, Denominator, f);
    return f;
}
/*
 *
 */
int GetMotorPulsePerMeter(MH_KNIT *p)
{
    MOTOR_DRIVE *pmotor = &p->motor_drive;
  /*  int Denominator = pmotor->motor_pitch * pmotor->motor_drive_wheel
            * pmotor->motor_belt * pmotor->motor_elec_molecular;

    int molecular = pmotor->motor_coder * pmotor->motor_pass_wheel * 4
            * pmotor->motor_elec_denominator;
    int ret = 0;
    if(Denominator)
    {
        ret = molecular / (float) Denominator * 1000;//pulse/m
    }*/
    /*
    *  1000*60*被动轮齿数 / 传动轮齿数*同步带齿数*皮带齿距
    */
    int ret = 0;
    int Denominator = pmotor->motor_drive_wheel * pmotor->motor_pitch *   pmotor->motor_belt;
    int molecular =  1000 * 60 * pmotor->motor_pass_wheel;
    if(Denominator)
    {
        ret = molecular /(float)Denominator;
    }
    return ret;
}


int SetJustResetJijia(MH_KNIT *p, ushort state)
{
    int ret = 0;
     if(p->communication == COMM_SRAM)
    {
    }
    else
    {
        ret = MacJustResetJijia(p->pmac, state);
    }
    return ret;
}

int SetResetJijia(MH_KNIT *p, ushort state)
{
    int ret = 0;
     if(p->communication == COMM_SRAM)
    {
    }
    else
    {
        ret = MacResetJijia(p->pmac, state);
    }
    return ret;
}

int SetUpgradeJijia(MH_KNIT *p, ushort state)
{
    int ret = 0;
    if(p->communication == COMM_SRAM)
    {
    }
    else
    {
        ret = MacUpgradeJijia(p->pmac, state);
    }
    return ret;
}

int GetUpgradeJijia(MH_KNIT *p, uchar *state, uchar *percent)
{
    int ret = 0;
     if(p->communication == COMM_SRAM)
    {
    }
    else
    {
        ret = MacGetUpgradeFeedback(p->pmac, state, percent);
    }
    return ret;
}

int GetSysTimeJijia(MH_KNIT *p, int *time_set, int *time_id)
{
    int ret = 0;
     if(p->communication == COMM_SRAM)
    {
    }
    else
    {
        ret = MacGetSysTime(p->pmac, time_set, time_id);
    }
    return ret;
}

int GetSysId(MH_KNIT *p, char *id)
{
    int ret = 0;
    if(p->communication == COMM_SRAM)
    {
    }
    else
    {
        ret = MacGetSysId(p->pmac, id);
    }
    return ret;
}



/********************************************************************************
 * modify the param of machine below
 ********************************************************************************/
/*
 * send rock data to machine
 */
int SendRockParam(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            ROCK_POS rock_pos;
            //rock
            memset(&rock_pos.pos[0][0], 0, sizeof(ROCK_POS));
            float f1 = GetRockPulsePerSilk(p);
            float type = p->inch_type;
            float f2 =  CM_PER_INCH * 1000.0 / type;
            float f = f2 * f1;
            int i = 0;

            rock_pos.pos[0][0] = p->rock_right_tran_com[0] * f1; //tran
            rock_pos.pos[2][0] = p->rock_right_star_com[0] * f1;


            for(i = 1; i < 16; i++)
            {
                rock_pos.pos[0][i] = rock_pos.pos[0][0] + f * i + p->rock_right_tran_com[i] * f1;
                rock_pos.pos[2][i] = rock_pos.pos[2][0] + f * i + p->rock_right_star_com[i] *f1;
            }
            for(i = 0; i < 16; i++)
            {
                 rock_pos.pos[0][16 + i] = f * (i + 1) - rock_pos.pos[0][0] + p->rock_left_tran_com[i] * f1;
                 rock_pos.pos[2][16 + i] = f * (i + 1) - rock_pos.pos[2][0] + p->rock_left_star_com[i] * f1;

                  rock_pos.pos[1][i] = f * (i + 1) + p->rock_right_com[i] * f1;
                  rock_pos.pos[1][i + 16] = f * (i + 1) + p->rock_left_com[i] * f1;
            }
            rock_pos.zero = 100;

 /*       float fcell = GetRockPulsePerSilk(p); //pulse/0.01mm  //打印出来printf:0.000013
            float fmm = 0.0;
            if(p->inch_type > 0.8)
            {
                fmm = CM_PER_INCH * 1000.0 / p->inch_type; //0.01mm/needle
            }
            float fneedle = fcell * fmm;//pulse/needle    printf: :0.011042
            rock_pos.zero = p->rock_right_com[0] * fcell;  // printf: 0

            rock_pos.pos[0][0] = fneedle / 2 + p->rock_right_tran_com[0] * fcell; //tran
            rock_pos.pos[2][0] = fneedle / 2 + p->rock_right_star_com[0]* fcell;

            int i = 0;
            for(i = 0; i < 15; ++i)
            {
                rock_pos.pos[0][i + 1] = rock_pos.pos[0][0] + fneedle * (i + 1) + p->rock_right_tran_com[i + 1] * fcell;
                rock_pos.pos[1][i] = rock_pos.zero + fneedle * (i + 1) + p->rock_right_com[i + 1] * fcell;

                rock_pos.pos[0][i + 16] = fneedle * (i + 1) - rock_pos.pos[0][0] + p->rock_left_tran_com[i + 1]* fcell;
                rock_pos.pos[1][i + 16] = fneedle * (i + 1) - rock_pos.zero + p->rock_left_com[i + 1] * fcell;

                rock_pos.pos[2][i + 1] = rock_pos.pos[2][0] + fneedle * (i + 1) + p->rock_right_star_com[i + 1] * fcell;
                rock_pos.pos[2][i + 16] = fneedle * (i + 1) - rock_pos.pos[2][0] + p->rock_left_star_com[i + 1] * fcell;
            }*/


            ret = MacSendRockData(p->pmac, &rock_pos);
        }
    }
    return ret;
}
/*
 * send system param to machine
 */
int SendSysParam(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication ==COMM_SRAM)  //**
        {
            uchar data[SEND_MAX_SIZE] = { 0 };
            GetBaseData(p, (BASE_DATA*) &data[4]);
            GetSysData(p, (SYS_DATA*) &data[404]);
            ret = SramSendSysData(p->pmac, data, sizeof(data), SEND_BASE_BIT);
        }
        else
        {
            SYS_PARAM sys_param;
            memset(&sys_param, 0, sizeof(sys_param));
            sys_param.type.Integer = (int) p->inch_type;
            sys_param.type.Float = (int) (p->inch_type - sys_param.type.Integer);
            if(p->inch_type > 0.8)
            {
                sys_param.len = p->total_needle / p->inch_type;
            }
            sys_param.total_pulse = p->belt_right_com - p->belt_left_com;
            sys_param.zero_pulse = p->needle_zero;
            sys_param.lgrz = p->left_yarn_right_com;
            sys_param.lglz =  p->left_yarn_left_com;
            sys_param.rgrz = GetRightYarnRightCom(p);
            sys_param.rglz = GetRightYarnLeftCom(p);
            sys_param.qdb_enable = p->board_enable;
            sys_param.rock_speed = p->rock_speed;
            sys_param.sink_enable = p->sink_enable;
            if(p->shake_enable)
            {
                sys_param.shake_level = p->front_shake_sense;
            }
            else
            {
                sys_param.shake_level = 0;
            }
            sys_param.rock_com = GetRockPulsePerSilk(p);
            sys_param.motor_com = GetMotorPulsePerMeter(p);
            sys_param.xzq_time = p->select_high_voltage;
            //sys_param.mac_no
            sys_param.dumu_reset = p->dumu_reset_speed;
            sys_param.dumu_work = p->dumu_work_speed;
            sys_param.xzq_2left_comp = p->select_left_com;
            sys_param.xzq_2right_comp = p->select_right_com;
            //sys_param.inch_plus
            sys_param.max_speed = GetMacHighLineSpeed(p);
            //sys_param.retain1
            // sys_param.can_work;
            // sys_param.sink_work;
            // sys_param.retain2;
            sys_param.xzq_resel = p->select_repeat_enable;
            sys_param.rab_compe = p->roll_open_com;
            ret = MacSendSysParam(p->pmac, &sys_param);
        }
    }
    return ret;
}
/*
 * send work param to machine
 */
int SendWorkParam(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            WORK_PARAM work_param;
            memset(&work_param, 0, sizeof(work_param));
            work_param.start_needle = p->start_needle;
            work_param.ret_len = p->mac_ret_len;
            work_param.ext_inter = p->ext_piece_inter;
            work_param.ext_enable = TRUE;
            work_param.hd_end = p->hangding_end;
            work_param.hd_start = p->hangding_start;
            work_param.hd_num = p->hangding_times;
            if(p->save_screen_enable)
            {
                work_param.save_screen = p->save_screen_time;
            }
            else
            {
                work_param.save_screen = 0;
            }
            work_param.low_speed = p->mac_low_speed;
            work_param.knot_speed = 0;
            work_param.limit_speed = p->mac_mid_speed;
            work_param.knot_low_zhuan = 0;
            work_param.auto_zero_cloth = p->auto_zero_cloth;
            work_param.rab_percent = p->rab_percent;
            work_param.left_feeder_enable = p->left_feeder_enable;
            work_param.right_feeder_enable = p->right_feeder_enable;
            work_param.feeder_mode = p->yarn_feeder_mode;
            work_param.board_mode = 0;
            work_param.board_yarn = 0;
            work_param.cam_lock = p->cam_lock;;
            work_param.empty_speed = p->mac_empty_speed;
            work_param.rab_speed =0;
            work_param.rab_open_speed = 0;
            work_param.dumu_add_for_stop = p->dumu_add_percent;
            work_param.second_dumu_needle = p->second_dumu_com;
            work_param.null_data = 0;
            work_param.yarn_take_mode = p->yarn_take_mode;
            work_param.auto_line_set = p->auto_line_set;
            work_param.return_state = p->return_state;
            work_param.rab_copy_enable = p->copy_rab_enable;
            ret = MacSendWorkParam(p->pmac, &work_param);
        }
    }
    return ret;
}
/*
 * send work param to machine
 */
int SendHideParam(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
           // RunChangeParam(p);
        }
        else
        {
            HIDE_PARAM hide_param;
            memset(&hide_param, 0, sizeof(HIDE_PARAM));
            hide_param.shield_probe_len = p->probe_shield;
            hide_param.tran_dumu_enable = p->knit_state.tran_dumu_enable;
            ret = MacSendHideParam(p->pmac, &hide_param);
        }
    }
    return ret;
}

/*
 *
 */
int SendPartsParam(MH_KNIT *p)
{

    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication != COMM_SRAM)
        {
            MAC_PARAM mac_param;
            memset(&mac_param, 0, sizeof(MAC_PARAM));
            mac_param.select1_to_mac_left_len = p->select1_to_mac_left_len * p->inch_type / MM_PER_INCH;
            mac_param.select2_to_mac_left_len = p->select2_to_mac_left_len * p->inch_type / MM_PER_INCH;
            mac_param.select3_to_mac_left_len = p->select3_to_mac_left_len * p->inch_type / MM_PER_INCH;
            mac_param.select4_to_mac_left_len = p->select4_to_mac_left_len * p->inch_type / MM_PER_INCH;
            mac_param.select_piece = GetSelectPiece(p);
            mac_param.select_mode = GetSelectArray(p);
            mac_param.rock_mode = GetRockMode(p);
            mac_param.rock_zero_dir = GetRockZeroDir(p);
            mac_param.language = GetLanguage(p);
            mac_param.red_light_time = GetRedLightTime(p);
            mac_param.select_sect = GetSelectSect(p);
            mac_param.sever_type = GetSeverType(p);
            mac_param.probe_to_mac_left_len = GetProbeDistance(p);
            mac_param.select_start = GetSelectStartSect(p);
            mac_param.yarn_type = GetYarnType(p);
            mac_param.sink_dir = GetSinkDir(p);
            mac_param.dumu1_to_mac_left_len = GetDumu1ToMacLeftLen(p);
            mac_param.dumu2_to_mac_left_len = GetDumu2ToMacLeft(p);
            mac_param.dumu3_to_mac_left_len = GetDumu3ToMacLeftLen(p);
            mac_param.dumu4_to_mac_left_len = GetDumu4DistanceToMacLeft(p);
            mac_param.fuel_gab = GetFuelGab(p);
            mac_param.fuel_time = GetFuelTime(p);
            mac_param.select5_to_mac_right_len = GetSelect5ToMacRightLen(p);
            mac_param.select6_to_mac_right_len = GetSelect6ToMacRightLen(p);
            mac_param.mac_width = GetMacWidth(p);
            mac_param.sink1_to_mac_left_len = GetSink1ToMacLeftLen(p);
            mac_param.sink2_to_mac_left_len = GetSink2ToMacLeftLen(p);
            ret = MacSendPartsParam(p->pmac, &mac_param);
        }
    }
    return ret;
}
/*
 *
 */
int SendAlarmMask(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication != COMM_SRAM)
        {
            ALARM_MASK *alarm = (ALARM_MASK *)&p->alarm_mask;
            ret = MacSendAlarmMask(p->pmac, alarm);
        }
    }
    return ret;
}
/*
 * send exchanged yarn to machine
 */
int SendExchangedYarn(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            YARN_EXCHANGE yarn_exchange;
            memset(&yarn_exchange, 0, sizeof(YARN_EXCHANGE));
            int i;
            for(i = 0; i < GAUZE_NUM; ++i)
            {
                yarn_exchange.data[i][0] = p->gauze_exchange[0][i];
                yarn_exchange.data[i][1] = p->gauze_exchange[1][i];
            }
            ret = MacSendExchangedYarn(p->pmac, &yarn_exchange);
        }
    }
    return ret;
}
/*
 * send control line to machine
 * param line: 1 start
 */
int SendControlLine(MH_KNIT *p, ushort line)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            if(line > 0 && line <= p->total_line)
            {
                KNIT_PAGE *con = &p->knit_page[line - 1];
                ret = MacSendConLine(p->pmac, con, p->total_line, line);
            }
        }
    }
    return ret;
}
/*
 * send control data to machine
 */
int SendControlData(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok && p->communication != COMM_SRAM)
    {
        ret = MacSendControlData(p->pmac, p->knit_page_head, p->total_line);
    }
    return ret;
}
 /*
 *
 */
int SendFlowerData(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok && p->communication != COMM_SRAM)
    {
        ret = MacSendFlowerData(p->pmac, p->flower_data, p->flower_width, p->flower_height);
    }
    return ret;
}
/*
 * send speed data to machine
 */
int SendSpeedParam(MH_KNIT *p)
{

    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            MH_SPEED speed;
            memset(&speed, 0, sizeof(MH_SPEED));
            memcpy(&speed.rude_speed, p->rude_dumu,sizeof(p->rude_dumu));    //二段度目
            memcpy(&speed.master_speed, p->mac_speed, sizeof(p->mac_speed));     //速度
            memcpy(&speed.switch_speed, p->switch_speed, sizeof(p->switch_speed));    //开合罗拉
            memcpy(&speed.rab_speed, p->rab_speed, sizeof(p->mac_speed));//高罗拉
            memcpy(&speed.roll_speed, p->roll_speed, sizeof(p->roll_speed));//低罗拉

            ret = MacSendSpeedParam(p->pmac, &speed);
        }
    }
    return ret;
}
/*
 * send dumu data to machine
 */
int SendDumuParam(MH_KNIT *p)
{
   int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            MH_DUMU dumu;
            memset(&dumu, 0, sizeof(MH_DUMU));
            if(p->sys_info.cam == 1)
            {
                memcpy(dumu.f1, p->dumu_value[0][0], sizeof(p->dumu_value[0][0]));
                memcpy(dumu.f2, p->dumu_value[0][1], sizeof(p->dumu_value[0][1]));
                memcpy(dumu.b1, p->dumu_value[1][0], sizeof(p->dumu_value[1][0]));
                memcpy(dumu.b2, p->dumu_value[1][1], sizeof(p->dumu_value[1][1]));
            }
            else
            {
                memcpy(dumu.f1, p->dumu_value[0][0], sizeof(p->dumu_value[0][0]));
                memcpy(dumu.f2, p->dumu_value[0][1], sizeof(p->dumu_value[0][1]));
                memcpy(dumu.f3, p->dumu_value[0][2], sizeof(p->dumu_value[0][2]));
                memcpy(dumu.f4, p->dumu_value[0][3], sizeof(p->dumu_value[0][3]));
                memcpy(dumu.b1, p->dumu_value[1][0], sizeof(p->dumu_value[1][0]));
                memcpy(dumu.b2, p->dumu_value[1][1], sizeof(p->dumu_value[1][1]));
                memcpy(dumu.b3, p->dumu_value[1][2], sizeof(p->dumu_value[1][2]));
                memcpy(dumu.b4, p->dumu_value[1][3], sizeof(p->dumu_value[1][3]));
            }

            ret = MacSendDumuParam(p->pmac, &dumu);
        }
    }
    return ret;
}
/*
 * send dmbz param to machine
 */
int SendDmbzParam(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            STITCH_AMEND sa;
            sa.f1 = p->dmbz_single_com[0][0];
            sa.f2 = p->dmbz_single_com[0][1];
            sa.f3 = p->dmbz_single_com[0][2];
            sa.f4 = p->dmbz_single_com[0][3];
            sa.b1 = p->dmbz_single_com[1][0];
            sa.b2 = p->dmbz_single_com[1][1];
            sa.b3 = p->dmbz_single_com[1][2];
            sa.b4 = p->dmbz_single_com[1][3];
            memset(sa.all, 0, sizeof(sa.all));
            ret = MacSendDmbzParam(p->pmac, &sa);
        }
    }
    return ret;
}
/*
 * send cloth num
 */
int SendClothNum(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            ret = MacSetClothNum(p->pmac, p->done_cloth, p->total_cloth);
        }
    }
    return ret;
}
/*
 * send yarn stop
 */
int SendYarnStopParam(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            YARN_STOP yarn_stop;
            memset(&yarn_stop, 0, sizeof(yarn_stop));
            int i = 0;
            for(i = 0; i < 8; ++i)
            {
                memcpy(yarn_stop.pos[i][0], p->yarn_stop[0][i],
                        sizeof(p->yarn_stop[0][i]));
                memcpy(yarn_stop.pos[i][1], p->yarn_stop[1][i],
                        sizeof(p->yarn_stop[1][i]));
            }
            ret = MacSendYarnStopParam(p->pmac, &yarn_stop);
        }
    }
    return ret;
}
/*
 *
 */
 int SendJijiaUpgradeFile(MH_KNIT *p, uchar *data, int file_size)
 {
     int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            //
        }
        else
        {
          ret = MacSendJijiaUpgrade(p->pmac, data, file_size);
        }
    }
    return ret;
 }


/*
 * @param value: 以mm为单位的距离
 * @note: 将mm转换为针数
 */
int yb_transform_rudeadv(MH_KNIT *p, int value)
{
    int rude_num = 10;

    int len = p->inch_type * value / 25.4;

    len += rude_num / 4;


    return len;
}
/*
 * @param value: 以mm为单位的距离
 * @note: 将mm转换为针数
 */
int yb_transform_dumu_adv(MH_KNIT *p, int speed, int value)
{
    int len = p->inch_type * value / 25.4;
    /* 机头实际速度(m/s) */
    float real_speed = 0.03 + 0.014 * (speed - 1);
    /* 机头在度目动作的时间行走的距离,度目电机动作的时间按280us一步，500步计算 */
    float distance = real_speed * 0.14;

   ushort crg_ver = 0;//GetCarriageVersion();

    if ((crg_ver & 0x0F00) == 0x400)
    {
        return len;
    }
    return (int)(distance * 1000 / 25.4 * p->inch_type) + len;
}
/*
 * @
 */
int SendBeforeCom(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication != COMM_SRAM)
        {
            int i = 0, j = 0;
            YARN_BEFORE_NEEDLE yarn_before_needle;

            for (i = 0; i < 6; i++)
            {
                for (j = 0; j < 100; j++)
                {
                    yarn_before_needle.data[i][j] = (char)(p->yarn_set[i][j] / 25.4 *  p->inch_type);
                }
            }

            for (i = 6; i < 8; i++)
            {
                for (j = 0; j < 100; j++)
                {
                   yarn_before_needle.data[i][j] =yb_transform_rudeadv(p, p->yarn_set[i][j]);
                }
            }

            for (i = 8; i < 12; i++)
            {
                for (j = 0; j < 100; j++)
                {
                     yarn_before_needle.data[i][j] =yb_transform_dumu_adv(p, j, p->yarn_set[i][j]);
                }
            }

            ret = MacSendBeforeCom(p->pmac, &yarn_before_needle);
        }
    }
    return 0;
}
/*
 * @
 */
 int SendBeforeData(MH_KNIT *p, int num, int offset)
 {
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication != COMM_SRAM)
        {
            int i = 0;
            int group = offset/100;
            int index = offset%100;
            int size = 2*num * sizeof (char);
            float needle = p->inch_type;
            short *data = (short*)malloc(size);
            char *send_data = (char*)malloc(size);
            memcpy(data, &p->yarn_set[group][index], size);

            if (offset >= 6 * 100 && offset < 8 * 100)
            {
                for (i = 0; i < num; i++)
                {
                    send_data[i] = (char)yb_transform_rudeadv(p, (int)data[i]);
                    printf("data[%d] = %d, send_data[%d] = %d\n", i, data[i], i, send_data[i]);
                }
            }
            else if (offset >= 8 * 100 && offset < 12 * 100)
            {
                for (i = 0; i < num; i++)
                {
                    int speed = (offset + i - 800) % 100;
                    send_data[i] = (char)yb_transform_dumu_adv(p, speed , data[i]);
                    printf("data[%d] = %d, send_data[%d] = %d\n", i, data[i], i, send_data[i]);
                }
            }
            else
            {
                for (i = 0; i < num; i++)
                {
                    send_data[i] = (char)(data[i] / 25.4 * needle);
                    printf("data[%d] = %d, send_data[%d] = %d\n", i, data[i], i, send_data[i]);
                }
            }
       //     ret = MacSendYarnBeforeSet(p->pmac, send_data, num, offset);
            free(data);
            free(send_data);
        }
    }
    return ret;
 }
/*
 * send extent piece data
 */
int SendExtentPieceData(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            RunChangeParam(p);
        }
        else
        {
            int i;
            EXT_PIECE ext_piece;
            ext_piece.src_enable = p->ext_piece_enable[0];
            for(i = 0; i < 8; ++i)
            {
                ext_piece.piece[i].enable = p->ext_piece_enable[i + 1];
                memcpy(ext_piece.piece[i].yc, p->ext_piece_yarn[i + 1],
                        sizeof(p->ext_piece_yarn[i + 1]));
            }
            ret = MacSendExtentPieceData(p->pmac, &ext_piece);
        }
    }
    return ret;
}

/*
 * send sink param to machine
 */
int SendSinkParam(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            //
        }
        else
        {
            SINK_DATA data;
            memset(&data, 0, sizeof(SINK_DATA));
            int i = 0, j = 0;
            for(i = 0; i < 8; i ++)
            {
                for(j = 0; j < 10; ++j)
                {
                    data.section[i].data[j] = GetRunSink(p, i, j);
                }
            }
            data.fzero = GetFrontSinkZero(p);
            data.bzero = GetBackSinkZero(p);
            ret = MacSendSinkParam(p->pmac, &data);
        }
    }
    return ret;
}
/*
 * send cam param to machine
 */
int SendCamParam(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            //
        }
        else
        {
            CAM_DATA data;
            memset(&data, 0, sizeof(CAM_DATA));
            int i = 0, j = 0;
            for(i = 0; i < 4; i ++)
            {
                for(j = 0; j < 8; ++j)
                {
                    data.value[i][j] = GetCamSettingState(p, i, j);
                }
            }
            ret = MacSendCamParam(p->pmac, &data);
        }
    }
    return ret;
}

/*
 * send yarnfeeder param to machine
 */
int SendYarnFeederParam(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            //
        }
        else
        {
            YARNFEEDER_DATA data;
            memset(&data, 0, sizeof(YARNFEEDER_DATA));
            data.start_time = GetFeederStartWait(p);
            int i = 0;
            for(i = 0; i < sizeof(data.speed); i++)
            {
                data.speed[i] = GetYarnFeederSpeed(p, i);
            }
            ret = MacSendYarnFeederParam(p->pmac, &data);
        }
    }
    return ret;
}

/*
 * send board set param to machine
 */
 int SendBoardParam(MH_KNIT *p)
 {
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            //
        }
        else
        {
            BOARD_PARAM data;
            memset(&data, 0, sizeof(BOARD_PARAM));
        /*    data.yarn_left_len = GetYarnLeftOut(p);
            data.yarn_right_len = GetYarnRightOut(p);
            data.gou_position = GetBoardParamHighPos(p);
            data.safe_position = GetBoardParamSafePos(p);
            data.roll_close_position = GetBoardParamRollClosePos(p);
            data.roll_out_cercle = GetBoardParamRollHookOpenPos(p);
            data.board_up_start_speed = GetBoardParamUpStartSpeed(p);
            data.board_up_add_speed = GetBoardParamUpQuickSpeed(p);
            data.board_up_slow_speed = GetBoardParamUpSlowSpeed(p);
            data.board_up_add_position = GetBoardParamUpQuickPos(p);
            data.board_up_slow_position = GetBoardParamUpSlowPos(p);
            data.rab_close_speed = GetRabCloseSpeed(p);
            data. forfex_enable = GetBoardParamCSSyn(p);

            data.out_cercle_later = GetBoardParamHookOpenDelay(p);
            data.scissor_times = GetBoardParamScissorTimes(p);
            data.rab_out_cercle = GetBoardParamRabHookOpenPos(p);
            data.down_safer_speed = GetBoardParamToSafeSpeed(p);
            data.board_reset_speed = GetBoardParamResetSpeed(p);
            data.out_cercle_down_speed = GetBoardParamDownHookOpenSpeed(p);
            data.board_slow_position = GetBoardParamDownSlowPos(p);
            data.board_slow_speed =GetBoardParamDownSlowSpeed(p);
            data.rab_speed = GetBoardParamDropRollSpeed(p);
            data.rab_timer = GetBoardParamDropWaitTime(p);
            data.board_up_position = GetBoardParamUpWaitPos(p);
            data.rab_close_position = GetBoardParamRabClosePos(p);
            data.rab_open_speed = GetRabOpenSpeed(p);*/
            ret = MacSendBoardParam(p->pmac, &data);
        }
    }
    return ret;
 }
 /*
 * send  machine type
 */
 int SendMachineType(MH_KNIT *p)
 {
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            //
        }
        else
        {
            ret = MacSendMacType(p->pmac);
        }
    }
    return ret;
 }

/////////////////////////////////////////////////////////////////////////
/*
 * thread for probe data to send
 */
int SendParamThread(MH_KNIT *p)
{
    PMACHINE pmac = p->pmac;
    ushort state = 0, reset_state = 0;
   if(MacGetSysState(pmac, &state, &reset_state) < 0)
    {
        p->net_state = RUN_FB_NETMESSAGE_WARN;
        return -1;
    }
    /* 控制文件 */
    if(!p->knit_page)
    {
        return -1;
    }
    if(SendControlData(p) < 0)
    {
        return -1;
    }
    /* 花样文件 */
    if(!p->flower_data)
    {
        return -1;
    }
    if(SendFlowerData(p) < 0)
    {
        return -1;
    }
    /* 度目文件 */
    if(SendDumuParam(p) < 0)
    {
       return -1;
    }
    /* 速度文件 */
    if(SendSpeedParam(p) < 0)
    {
        return -1;
    }
    /*纱嘴停放点文件*/
    if(SendYarnStopParam(p) < 0)
    {
        return -1;
    }
    /*纱嘴提前量文件*/
    if(SendBeforeCom(p) < 0)
    {
        return -1;
    }
    /*续片文件*/
    if(SendExtentPieceData(p) < 0)
    {
        return -1;
    }
    /*纱嘴替换文件*/
    if(SendExchangedYarn(p) < 0)
    {
        return -1;
    }
    /* 系统参数 */
    if(SendSysParam(p) < 0)
    {
        return -1;
    }
    /* 工作参数 */
    if(SendWorkParam(p) < 0)
    {
        return -1;
    }
    /* 机器参数 */
    if(SendPartsParam(p) < 0)
    {
        return -1;
    }
    /* 摇床 */
    if(SendRockParam(p) < 0)
    {
       return -1;
    }
    /* 件数设置 */
    if(SendClothNum(p) < 0)
    {
        return -1;
    }
    /* 报警屏蔽 */
    if(SendAlarmMask(p) < 0)
    {
        return -1;
    }
    /* 度目补正*/
    if(SendDmbzParam(p) < 0)
    {
        return -1;
    }
    /* 隐藏参数*/
    if(SendHideParam(p) < 0)
    {
        return -1;
    }
    /*辛克电机*/
    if(SendSinkParam(p) < 0)
    {
        return -1;
    }
     /*送纱速度设置*/
    if(SendYarnFeederParam(p) < 0)
    {
        return -1;
    }
      /*起底板参数设置*/
    if(SendBoardParam(p) < 0)
    {
        return -1;
    }
    /*三角设置*/
    if(SendCamParam(p) < 0)
    {
        return -1;
    }
     /*机器类型*/
    if(SendMachineType(p) < 0)
    {
        return -1;
    }
    return 0;
}
/*
 * send knit data
 */
int SendGlobalParam(MH_KNIT *p)
{
    int ret = 0;
  //  if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            uchar data[SEND_MAX_SIZE] = { 0 };
            GetBaseData(p, (BASE_DATA*) &data[4]);
            GetSysData(p, (SYS_DATA*) &data[404]);
            int mask = SEND_CAM_BIT;
            int error = ReadRawFile(CARRIAGE_PARAM_FILE, &data[512], 124 + 28);
            if(error)
            {
                printf("read knit continue: %d\n", error);
            }
            else
            {
                mask |= SEND_KNIT_CONTINUE_BIT;
            }
            ret = SramSendSysData(p->pmac, data, sizeof(data), mask);
        }
        else
        {
            ret = SendParamThread(p);
        }
    }
    printf("Send global ret: %d\n", ret);
    return ret;
}
/*
 * get version info
 */
int GetVersionInfo(MH_KNIT *p, VERSION *pversion)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramGetVersionInfo(p->pmac, pversion);
        }
        else
        {
            ret = MacGetVersionInfo(p->pmac, pversion);
        }
    }
    return ret;
}
